Problem z wyświetleniem bazy danych z json

Odpowiedz Nowy wątek
2019-05-04 22:26

Rejestracja: 10 miesięcy temu

Ostatnio: 6 dni temu

0

Witam mam problem z wgraniem danych z pliku json. W przegladarce pokazuje sie tylko [] element tablicy. Ktos wie?

@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}

@Bean
CommandLineRunner runner(ServiceBooks serviceBooks){
return args -> {
// read JSON and load json
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<books>> typeReference = new TypeReference<List<books>>(){};
InputStream inputStream = TypeReference.class.getResourceAsStream("/dbjson/book.json");
try {
List<books> book = mapper.readValue(inputStream,typeReference);
serviceBooks.save((Books) book);
System.out.println("Books Saved!");
} catch (IOException e){
System.out.println("Unable to save books: " + e.getMessage());
}
};
}
}

Kontroler:
@RestController
@RequestMapping("/")
public class ControllerBooks {
//
ServiceBooks serviceBooks;

@Autowired
public ControllerBooks(ServiceBooks serviceBooks) {
this.serviceBooks = serviceBooks;
}

@GetMapping("/api/books")
public Iterable<Books> list() {
    return serviceBooks.list();
}

Serwis:
@Service
public class ServiceBooks {

RepositoryBooks repositoryBooks;

@Autowired
public ServiceBooks(RepositoryBooks repositoryBooks) {
this.repositoryBooks = repositoryBooks;
}

public Iterable<Books> list() {
    return repositoryBooks.findAll();
}

//
public Books save(Books books) {
return repositoryBooks.save(books);
}

public void zapisz (List<Books> book) {
    repositoryBooks.saveAll(book);
}

Repozytorium:
@Repository
public interface RepositoryBooks extends CrudRepository<Books,String> {

Plik Json(wyciałem kawałek):

{
"requestedUrl": "https://www.googleapis.com/bo[...]umes?q=java&maxResults=40",
"items": [
{
"kind": "books#volume",
"id": "7tkN1CYzn2cC",
"etag": "pfjjxSpetIM",
"selfLink": "https://www.googleapis.com/books/v1/volumes/7tkN1CYzn2cC",
"volumeInfo": {
"title": "A Hypervista of the Java Landscape",
"publisher": "InfoStrategist.com",
"industryIdentifiers": [
{
"type": "ISBN_13",
"identifier": "9781592432172"
},
{
"type": "ISBN_10",
"identifier": "1592432174"
}
],

Jeszcze raz - jaki jest właściwie problem? - Pinek 2019-05-04 22:28

Pozostało 580 znaków

2019-05-06 18:23

Rejestracja: 4 lata temu

Ostatnio: 28 minut temu

0

Odpaliłem projekt. Generalnie leci błąd przy zapisie, ale że Ty robisz try catch i printujesz tylko małą jego część to nie widzisz co się dzieje.

2019-05-06 18:22:29.580 ERROR 8043 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at application.demo.DemoApplication.main(DemoApplication.java:23) ~[classes/:na]
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
 at [Source: UNKNOWN; line: 1, column: 0]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.9.8.jar:2.9.8]
    at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4145) ~[jackson-databind-2.9.8.jar:2.9.8]
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000) ~[jackson-databind-2.9.8.jar:2.9.8]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3077) ~[jackson-databind-2.9.8.jar:2.9.8]
    at application.demo.DemoApplication.lambda$runner$0(DemoApplication.java:35) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    ... 5 common frames omitted

Napraw błąd i wtedy zobacz.

Pozostało 580 znaków

Odpowiedz

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0