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"]}
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){ ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With