将json发布到spring mvc控制器 [英] post json to spring mvc controller
本文介绍了将json发布到spring mvc控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
控制器签名(我也尝试过请求者):
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) {}
这是我的json:
{
"strToMatchA": "EN",
"strToMatchB": "lon"
}
不工作,我收到错误:
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 ?
当我用 @RequestBody
更改方法参数时,我得到以下内容错误:
When I change method parameters to be annotated with @RequestBody
I get the following error :
java.io.IOException: Stream closed
推荐答案
你的json很好但不是控制器签名。
创建一个包含与json匹配的setter的类。
使用它作为参数而不是字符串。
使用requestbody注释它。它应该工作。
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.
这篇关于将json发布到spring mvc控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文