简体   繁体   中英

How to send nested JSON input to RESTful web service in Java?

How can I send the following nested JSON input to REST web service and read the values in the service's class?

{
 "payload": {
   "objType": "",
      "props": {
        "propName1": "propValue1",
        "propName2": "propValue2"
      },
   "retProps": ["prop1", "prop2"]
   }
}

To read the JSON input which is not nested , I've created a class:

public class MessageInputJSON {

private String payload;
private String objType;
private String prop;
private String retProp;

    // Getters and Setters here
}

And to read the values, I have created the following class :

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)

public String readJSON(MessageInputJSON message) {

         String payload = message.getPayload();
         String objType = message.getObjType();

    }

But I do not know how to send the nested JSON input and read its value in Java

You need to type the transfer Object with the same hierarchy of the json, with your sample the input dto must look like this :

public class Payload {
 private String objType;
 Props PropsObject;
 ArrayList < Object > retProps = new ArrayList < Object > ();

Full object : https://codebeautify.org/json-to-java-converter/cb83de35

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