I am trying to set up my first REST webservice using the spring framework. 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.
I am using postman, set to POST, raw, application/JSON and then the following:
{"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:
"attendanceList" -> ABSENT (the default value)
"Jane Doe" -> PRESENT
"John MacDonald" -> PRESENT
"Fred Flinstone" -> ABSENT
And my 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>
. The JSON is clearly not such a map.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.