简体   繁体   中英

How to retrieve values from an object selected by REST API using JSON?

I'm using a REST API in back-end java to talk to javascript on front-end, everything through JSON. I have an entity called Store that have some private String attributes. There's also a class called Repo containing all previously inserted Store . I'm capable of retrieve a list with all Store objects from Repo containing all private Strings. Now I added a list of Fridge to the class Store , where Fridge is a class with simple private String attributes.

How I'm supposed to retrieve Fridge 's list from some Store object using REST API? Should I create a new route that receives a JSON Store and send back a Fridge 's list? Or I should create a route receiving 2 JSON objects, the first being my Store object and the second a simple string specifying what to send back( Store attributes or Fridge 's list)?

I'm newbie to REST API and JSON and I really don't know the development pattern around this technologies.

Relationship between Store and Fridge is important here and that is what will drive on how you will retrieve Fridge. From what I understand, a single store contains a list of Fridges, which means there is a one to many relationship between store and Fridge. From coding perspective, you can define a new REST API, that takes the Store ID (primary key) as input and in response returns the list of Fridges in that store.

@Get
@Path("/fridges")
public Fridge[] getFridges(@PathParam int storeID){
   //implementation
   //retrieve store object
   //store object will have your list of Fridges..
  //just return list of fridges..
}

Using JSON or XML is irrelevant.

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