繁体   English   中英

从JSON解析Map和String不起作用

[英]Parsing Map and String from JSON doesn't work

我正在尝试使用spring框架设置我的第一个REST Web服务。 甚至不打扰前端之前,我想先在Controller中设置Requestmapping,然后尝试使用Postman进行测试。

我正在使用邮递员,设置为POST,原始,应用程序/ JSON,然后设置以下内容:

{"attendanceList" : { 
    "Jane Doe": "PRESENT", 
    "John MacDonald": "PRESENT", 
    "Fred Flinstone": "ABSENT"},
"date":"2017-06-01"}

在我的控制器中,我有以下内容:

@RequestMapping(value = "/post", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public void setAttendence(@RequestBody Map<String, Attendence> attendenceList, String date) {
    //Implementation code
}

但是,当我将上述发布请求发送到服务器时,我得到了大小为4的地图:

"attendanceList" -> ABSENT (the default value)
"Jane Doe" ->  PRESENT
"John MacDonald" -> PRESENT
"Fred Flinstone" -> ABSENT

而且我的String date = null

为什么这不按我打算的方式工作? 我究竟做错了什么?

您告诉Spring,应将请求正文(即整个JSON)解析为Map<String, Attendence> JSON显然不是这样的映射。

而是应将其映射到诸如

public class Input {
    Map<String, Attendence> attendanceList;
    LocalDate date;

    // ...
}

当然,应该删除Spring忽略的String date参数。

暂无
暂无

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

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