Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

post json to spring mvc controller

Controller signature (I have tried as requestbody as well) :

@RequestMapping(value = "/Lame", method = RequestMethod.POST)
public
@ResponseBody
boolean getLame(@RequestParam String strToMatchA, @RequestParam String strToMatchB) {}

And this as my json :

{
"strToMatchA": "EN",
 "strToMatchB": "lon"
}

Not working, I receive the error :

org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'strToMatchA' is not present

Removing this first parameter from method signature then makes it work (the method gets called correctly), what should I be doing ?

When I change method parameters to be annotated with @RequestBody I get the following error :

java.io.IOException: Stream closed
like image 710
NimChimpsky Avatar asked Oct 07 '22 08:10

NimChimpsky


1 Answers

Your json is fine but not the controller signature. Create a class with setters matching the json. Use it as argument instead of your strings. Annotate it with requestbody. It should work.

like image 189
eugen Avatar answered Oct 10 '22 03:10

eugen