最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - post json to spring mvc controller - Stack Overflow

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论