I need to pass a list of values in the request body of POST
method but I get 400: Bad Request error
.
Below is my sample code:
@RequestMapping(value = "/saveFruits", method = RequestMethod.POST,
consumes = "application/json")
@ResponseBody
public ResultObject saveFruits(@RequestBody List<String> fruits) {
...
}
The JSON I am using is: {"fruits":["apple","orange"]}
This question is related to
spring
rest
spring-mvc
I had the same use case, You can change your method defination in the following way :
@RequestMapping(value = "/saveFruits", method = RequestMethod.POST,
consumes = "application/json")
@ResponseBody
public ResultObject saveFruits(@RequestBody Map<String,List<String>> fruits) {
..
}
The only problem is it accepts any key in place of "fruits" but You can easily get rid of a wrapper if it is not big functionality.
You can pass input as ["apple","orange"]
if you want to leave the method as it is.
It worked for me with a similar method signature.
Source: Stackoverflow.com