简体   繁体   中英

Spring boot External API to JPA Database Returns Null

Hi I'm trying to call an external API from my spring boot application and save to response to my local MySQL database, but the response returns null.

My Entity Class:

public class Book {
@Id
@GeneratedValue(
        strategy = GenerationType.AUTO
)
private Long id;
private String bookName;
private LocalDate publishYear;
private Integer stock;
private String ISBN;
private String bib_key;
private String info_url;
private String preview;
private String preview_url;
private String thumbnail_url;

}

My Service:

public Book getExternal(String isbn) {
    String uri = "https://openlibrary.org/api/books?bibkeys=ISBN:" + isbn + "&format=json";
    String ISBNCode = "ISBN:" + isbn;
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> externalBook = restTemplate.getForEntity(uri, String.class);
    JSONObject jsonObject = new JSONObject(externalBook);
    Book entity = new Book();
    jsonObject.put(ISBNCode, entity);
    bookRepository.saveAndFlush(entity);
    return entity;
}

When I try to print the response as String everyting is okay and I can see the response, but when I try to save this response to my database as Book entitity, everything returns null. And here is an example of a sample response:

{
"ISBN:1931498717": {
"bib_key": "ISBN:1931498717",
"info_url": "https://openlibrary.org/books/OL34424821M/Don't_Think_of_an_Elephant!",
"preview": "borrow",
"preview_url": "https://archive.org/details/dontthinkofeleph0000lako",
"thumbnail_url": "https://covers.openlibrary.org/b/id/12039210-S.jpg"
}
}

I tried lot's of solutions include that ones answered on the Stackoverflow but my JPA is still null.

{
    "id": 42,
    "bookName": null,
    "publishYear": null,
    "stock": null,
    "bib_key": null,
    "info_url": null,
    "preview": null,
    "preview_url": null,
    "thumbnail_url": null,
    "kind": null,
    "isbn": null
}

Did you try to debug or print "entity" object. I think it is null. I recommend you to use library like Gson to convert json to your entity class.

There are number of issue with this code:

  • The structure of JSON received from external service is not direclty correlated with Book. In the example JSON the data is a single-element entry with ISBN as the key, and everything else is the value. So this JSON cannot be directly converted into the Book object.

  • The code jsonObject.put(ISBNCode, entity) does not do what the author of the post was hope to get (as I see it). It does not assign values from the JSON object to the Book fields. It just "pushes" a new entry into the JSON with the value of an 'empty' Book.

  • And then finally the 'empty' Book object is persisted, which assigns an ID, but all the other fields are null, so the result is expected.

To fix the problem I suggest to do the following:

  1. Introduce a DTO (ex. BookDTO ) with the proper structure according the the example JSON, which might also include a Book field
  2. Use the Jackson mapper to convert the JSON to that DTO (ex. mapper.readValue(externalBook, BookDTO.class) )
  3. Build or get the Book instance from that DTO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM