简体   繁体   English

Spring中将Requestbody映射到JSONObject

[英]Mapping Requestbody to JSONObject in Spring

I have a class as below.我有一个 class,如下所示。

class ExampleBean{
   public String Name;
   public JSONObject data;
}

And i have @GET handler which is as follows:我有 @GET 处理程序,如下所示:

@GET
@Consumes({MediaType.APPLICATION_JSON})
public Response getData(ExampleBean dataBean)
{
    // some usage code here
}

I want following json to be mapped to the ExmampleBean:我想将以下 json 映射到 ExmampleBean:

{
  "Name":"Example",
  "data":{
       "hello":"world",
       "some":"value"
   }
 }

Everything works perfectly if data was a type which had two public fields named hello and some .如果data是一种具有两个名为hellosome的公共字段的类型,一切都会完美无缺。 But since data is a JSONObject which doesn't actually have those fields or relevant setters it ends up throwing Unrecognized field "hello" (Class JSONObject), not marked as ignorable at [Source: org.apache.catalina.connector.CoyoteInputStream@17b9a4bf; line: 31, column: 18]但是由于data是一个 JSONObject,它实际上没有那些字段或相关的设置器,它最终会抛出Unrecognized field "hello" (Class JSONObject), not marked as ignorable at [Source: org.apache.catalina.connector.CoyoteInputStream@17b9a4bf; line: 31, column: 18] Unrecognized field "hello" (Class JSONObject), not marked as ignorable at [Source: org.apache.catalina.connector.CoyoteInputStream@17b9a4bf; line: 31, column: 18]

ignore the data property when forming the object from incoming request. 根据传入请求形成对象时,请忽略data属性。

class ExampleBean{
   public String Name;
   @JsonIgnore
   public JSONObject data;
}

and change the rest service to accept data as a parameter from the incoming request. 并将其余服务更改为从传入请求中接受数据作为参数。

@GET
@Consumes({MediaType.APPLICATION_JSON})
public Response getData(@RequestBody ExampleBean dataBean,RequestParam("data") String data)
{

 JSONParser parser = new JSONParser();
 JSONObject json = (JSONObject) parser.parse(data);
    // some usage code here
}

or you can change the datatype of JsonObject data to String and form an object from the incoming request. 或者,您可以将JsonObject data的数据类型更改为String并根据传入的请求形成一个object

 class ExampleBean{
 public String Name;
 public String data;
 }

and later create an Json object from the data string 然后从数据字符串创建一个Json对象

      @GET
@Consumes({MediaType.APPLICATION_JSON})
public Response getData(@RequestBody ExampleBean dataBean)
{

 JSONParser parser = new JSONParser();
 JSONObject json = (JSONObject) parser.parse(dataBean.data);
    // converting the string data to jsonobject
}

Try with JsonNode. 尝试使用JsonNode。 It is working. 这是工作。

Class: com.fasterxml.jackson.databind.JsonNode 类别: com.fasterxml.jackson.databind.JsonNode

When you have the requirement to pass your @RequestBody as JSONObject or/and return the output as JSONObject.当您需要将 @RequestBody 作为 JSONObject 传递或/并返回 output 作为 JSONObject 时。 The most convenient approach I follow is:我遵循的最方便的方法是:

  1. Accept the @RequestBody as String (You can also accept as your Object Data Type) and internally in your Post Mapping method, convert to JSONObject.接受 @RequestBody 作为字符串(您也可以接受作为您的 Object 数据类型)并在您的 Post Mapping 方法内部,转换为 JSONObject。 For ex:例如:
@GET
@Consumes({MediaType.APPLICATION_JSON})
public String getData(@RequestBody String jsonObjectData)
{
    JSONObject parsedJsonData = new JSONObject(jsonObjectData);
    // other implementation
}  
  1. Before you return the JSONObject from your method, convert it to the String or your Object Data type在从您的方法返回 JSONObject 之前,将其转换为 String 或您的 Object 数据类型
return JsonObjectToReturn.toString(); // or you can parse it to your return Object data type.

I suggest you to use Map or JsonNode rather than JSONObject .我建议您使用MapJsonNode而不是JSONObject Because those are woking well with Jackson .因为那些与Jackson一起工作得很好。 Like follows,如下,

class ExampleBean {
   public String Name;
   public Map<String, String> data;
}

or或者

class ExampleBean {
   public String Name;
   public JsonNode data;
}

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

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