将json发布到spring mvc控制器


问题内容

控制器签名(我也曾尝试作为requestbody):

@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

从方法签名中删除第一个参数,然后使其起作用(正确调用该方法),我该怎么办?

当我更改要注释的方法参数时,出现@RequestBody以下错误:

java.io.IOException: Stream closed

问题答案:

您的json很好,但没有控制器签名。创建一个具有与json匹配的setter的类。使用它作为参数而不是您的字符串。用requestbody注释它。它应该工作。