Issue
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"]}
Solution
You are using wrong JSON. In this case you should use JSON that looks like this:
["orange", "apple"]
If you have to accept JSON in that form :
{"fruits":["apple","orange"]}
You'll have to create wrapper object:
public class FruitWrapper{
List<String> fruits;
//getter
//setter
}
and then your controller method should look like this:
@RequestMapping(value = "/saveFruits", method = RequestMethod.POST,
consumes = "application/json")
@ResponseBody
public ResultObject saveFruits(@RequestBody FruitWrapper fruits){
...
}
Answered By - wcislo
Answer Checked By - Mary Flores (JavaFixing Volunteer)