Reputation: 50
I'm trying to send json string to Spring controller, i'm getting 400 - bad request as response
i'm using Spring 4.0.3
This is my controller
@Controller
public class Customer{
@RequestMapping(value = "/apis/test", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody String test(HttpServletRequest params) throws JsonIOException {
String json = params.getParameter("json");
JsonParser jObj = new JsonParser();
JsonArray jsonObj = (JsonArray ) jObj.parse(json);
for(int i = 0; i < jsonObj.size(); i++) {
JsonObject jsonObject = jsonObj.get(i).getAsJsonObject();
System.out.println(jsonObject.get("name").getAsString());
}
return json;
}
}
Please help me to solve this
Upvotes: 0
Views: 1637
Reputation: 1444
@RequestMapping(value = "/apis/test", method = RequestMethod.GET, produces = "application/json")
The above means this is a HTTP GET method which does not normally accept data. You should be using a HTTP POST method eg:
@RequestMapping(value = "/apis/test", method = RequestMethod.POST, consumes = "application/json")
public @ResponseBody String test(@RequestParam final String param1, @RequestParam final String param2, @RequestBody final String body) throws JsonIOException {
then you can execute POST /apis/test?param1=one¶m2=two and adding strings in the RequestBody of the request
I hope this helps!
Upvotes: 1