[英]Parsing Map and String from JSON doesn't work
I am trying to set up my first REST webservice using the spring framework. 我正在尝试使用spring框架设置我的第一个REST Web服务。 Before even bothering with the frontend, I want to set up the Requestmapping in the Controller first and I am trying to test this out by using Postman.
甚至不打扰前端之前,我想先在Controller中设置Requestmapping,然后尝试使用Postman进行测试。
I am using postman, set to POST, raw, application/JSON and then the following: 我正在使用邮递员,设置为POST,原始,应用程序/ JSON,然后设置以下内容:
{"attendanceList" : {
"Jane Doe": "PRESENT",
"John MacDonald": "PRESENT",
"Fred Flinstone": "ABSENT"},
"date":"2017-06-01"}
In my Controller I have the following: 在我的控制器中,我有以下内容:
@RequestMapping(value = "/post", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public void setAttendence(@RequestBody Map<String, Attendence> attendenceList, String date) {
//Implementation code
}
However, when I send the above post request to the server, I get a map of size 4: 但是,当我将上述发布请求发送到服务器时,我得到了大小为4的地图:
"attendanceList" -> ABSENT (the default value)
"Jane Doe" -> PRESENT
"John MacDonald" -> PRESENT
"Fred Flinstone" -> ABSENT
And my String date = null 而且我的String date = null
Why is this not working the way I'm intending it to? 为什么这不按我打算的方式工作? What am I doing wrong?
我究竟做错了什么?
You're telling Spring that the request body, ie the whole JSON, should be parsed to a Map<String, Attendence>
. 您告诉Spring,应将请求正文(即整个JSON)解析为
Map<String, Attendence>
。 The JSON is clearly not such a map. JSON显然不是这样的映射。
It should instead be mapped to a class such as 而是应将其映射到诸如
public class Input {
Map<String, Attendence> attendanceList;
LocalDate date;
// ...
}
And of course, the String date
argument, that Spring ignores, should be removed. 当然,应该删除Spring忽略的
String date
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.