简体   繁体   English

如何将嵌套的 JSON 输入发送到 Java 中的 RESTful Web 服务?

[英]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?如何将以下嵌套的 JSON 输入发送到 REST Web 服务并读取服务类中的值?

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

To read the JSON input which is not nested , I've created a class:为了读取嵌套的 JSON 输入,我创建了一个类:

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但我不知道如何发送嵌套的 JSON 输入并在 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 :您需要使用与 json 相同的层次结构键入传输对象,对于您的示例,输入 dto 必须如下所示:

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

Full object : https://codebeautify.org/json-to-java-converter/cb83de35完整对象: https : //codebeautify.org/json-to-java-converter/cb83de35

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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