简体   繁体   中英

Consuming API and store results to database

I need to consume this API: https://api.punkapi.com/v2/beers and after consuming, I have to store it in the database, but only with next fields: internal id, name, description and mean value of the temperature. Any ideas or advice?

The simplest approach would be to have your Model only containing those attributes so that Spring only deserialize them from JSON to object. Something like the following:

public class YourModel {
   private long id;
   private String name;
   private String description;
}

Then in your Service you would have:

ResponseEntity<YourModel> response = restTemplate.getForEntity(url, YourModel.class);

You can then either save YourModel directly to the database (first you need to add some @Annotations if you want to rely on JPA) or you may build another more suited model to your use case.

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