【发布时间】:2017-01-28 21:30:26
【问题描述】:
我有一个名为 Example 的自定义对象,它有一个 org.json.JSONObject 作为 mongo 查询。
public class ExampleObject {
private JSONObject query;
private String[] projections;
private Number limit;
// Constructors, getters and setters
}
我正在使用这样的 REST 控制器:
@RestController
@RequestMapping("/example")
public class ExampleRestController {
@RequestMapping(value = "/search", method = RequestMethod.POST)
@ResponseBody
public String example(@RequestBody ExampleObject example) {
return "This is an example";
}
然后,我向 Postman 提出以下请求:
正文如下(我用http://jsonlint.com检查了JSON的有效性):
{
"query":{
"field1":"value1",
"field2":"value2"
},
"projections":["field3, field4"],
"limit":3
}
结果是:对象“示例”的投影和限制已正确设置,但查询为空 JSONObject(非空)。如果我不发送字段查询,则对象“example”上的 JSONObject 变量为 null。
我不明白为什么字段查询设置不正确。我想 Spring 将 @RequestBody json 映射到 ExampleObject。
【问题讨论】:
-
我怀疑 Jackson 不知道如何反序列化
JSONObject。你可以试试com.fasterxml.jackson.core.JsonNode吗? -
使用 JsonNode 可以正常工作!谢谢!!
标签: json spring spring-restcontroller