Spring:如何从POST主体获取参数? [英] Spring: How to get parameters from POST body?

查看:74
本文介绍了Spring:如何从POST主体获取参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用spring的网络服务,我必须从我的帖子请求的正文中获取参数?正文的内容如下: -

Web-service using spring in which I have to get the params from the body of my post request? The content of the body is like:-

source="mysource"

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

网络服务方法如下: -

And the web-service method looks like:-

@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);

    }

请告诉我如何从中获取参数身体?我可以把整个身体放在我的绳子里,但我认为这不是一个有效的方法。请告诉我如何进一步处理。

Please let me know how do I get the params from the body? I can get the whole body in my string but I don't think that would be a valid approach. Please let me know how do I proceed further.

推荐答案

您可以从请求中获取参数。

You can get param from request.

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

这篇关于Spring:如何从POST主体获取参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆