【发布时间】:2014-06-11 14:53:00
【问题描述】:
我需要将自定义对象的映射 Map<String, Set<Result>> 从前端发送到后端。
所以我认为应该可以构建 JSON,通过 Ajax 将其发送到 Controller 并通过 @RequestBody 注释在 Controller 中接收它,该注释应该将 json 绑定到对象。对吧?
控制器:
@RequestMapping(value = "/downloadReport", method = RequestMethod.POST)
public ResponseEntity<byte[]> getReport(@RequestBody Map<String, Set<Result>> resultMap)
{
Context context = new Context();
context.setVariable("resultMap", resultMap);
return createPDF("pdf-report", context);
}
JSON:
{
"result": [
{
"id": 1,
"item": {
"id": 3850,
"name": "iti"
},
"severity": "low",
"code": "A-M-01",
"row": 1,
"column": 1,
"description": "Miscellaneous warning"
}
]
}
型号:
public class Result {
private Integer id;
private Item item;
private String severity;
private String code;
private Integer row;
private Integer column;
private String description;
//getter & setters
//hashCode & equals
}
public class Item {
private Integer id;
private String name;
//getter & setters
}
在通过 ajax 发送像上面这样的 JSON 后,我从浏览器收到错误消息:
The request sent by the client was syntactically incorrect
如果我更改 JSON 以发送如下所示的空集,那么它可以工作,但我的地图当然有空集:
{"result": []}
那么,为什么我无法收到带有一组对象的填充地图?为什么绑定/解组不能按预期工作,我应该怎么做才能使其工作?
注意:
我正在使用杰克逊库并为@ResponseBody 的其他案例编组工作正常。问题在于通过@RequestBody 解组和绑定对象。
【问题讨论】:
标签: java json spring-mvc jackson