spring:如何从POST正文获取参数?


问题内容

使用spring的Web服务必须从发布请求的主体中获取参数?身体的内容就像:-

source=”mysource”

&json=
{
    "items": [
        {
            "username": "test1",
            "allowed": true
        },
        {
            "username": "test2",
            "allowed": false
        }
    ]
}

Web服务方法如下所示:

@RequestMapping(value = "/saveData", headers="Content-Type=application/json", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Boolean> saveData(@RequestBody String a) throws MyException {
        return new ResponseEntity<Boolean>(uiRequestProcessor.saveData(a),HttpStatus.OK);

    }

请让我知道如何从身体中获取参数?我可以把整个身体都放在弦上,但是我认为那不是有效的方法。请让我知道如何进一步进行。


问题答案:

您可以从请求获取参数。

@ResponseBody
public ResponseEntity<Boolean> saveData(HttpServletRequest request,
            HttpServletResponse response, Model model){
   String jsonString = request.getParameter("json");
}