java - post json to spring mvc controller - Stack Overflow

Controller signature (I have tried as requestbody as well) :@RequestMapping(value = "Lame",

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

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
Share Improve this question edited Oct 12, 2012 at 15:56 NimChimpsky asked Oct 12, 2012 at 15:26 NimChimpskyNimChimpsky 47.3k61 gold badges201 silver badges317 bronze badges 7
  • Did you pile with debugging enabled? Otherwise, your parameter names will not be available. – David Grant Commented Oct 12, 2012 at 15:29
  • @DavidGrant RequestParam will take parameter name defined in method signature. But I have tried whilst manually specify name too – NimChimpsky Commented Oct 12, 2012 at 15:32
  • 1 Which library are you using for object-mapping? Is it Jackson or something else? Is it on the classpath? Additionally, the request method for RESTFul controllers should basically be GET instead of POST. – Lion Commented Oct 12, 2012 at 15:34
  • @Lion jackson, it works for one paramter. Restful api should only use GET you say ? Debatable : stackoverflow./q/11522946/106261 – NimChimpsky Commented Oct 12, 2012 at 15:37
  • so my json looks right to everyone ? – NimChimpsky Commented Oct 12, 2012 at 15:38
 |  Show 2 more ments

1 Answer 1

Reset to default 4

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744616840a4584102.html

相关推荐

  • java - post json to spring mvc controller - Stack Overflow

    Controller signature (I have tried as requestbody as well) :@RequestMapping(value = "Lame",

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信