[英]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
是一种具有两个名为hello
和some
的公共字段的类型,一切都会完美无缺。 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:
我遵循的最方便的方法是:
@GET
@Consumes({MediaType.APPLICATION_JSON})
public String getData(@RequestBody String jsonObjectData)
{
JSONObject parsedJsonData = new JSONObject(jsonObjectData);
// other implementation
}
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
.我建议您使用
Map
或JsonNode
而不是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.