Java Spring:如何从 POST 正文中获取参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22163397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 14:00:30  来源:igfitidea点击:

Spring: How to get parameters from POST body?

javaspringrestspring-mvc

提问by Harinder

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:-

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

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.

请让我知道如何从身体获取参数?我可以在我的字符串中获取整个身体,但我认为这不是一种有效的方法。请让我知道我该如何继续。

采纳答案by Jason

You can get param from request.

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

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

回答by redzedi

You can bind the json to a POJO using MappingHymansonHttpMessageConverter. Thus your controller signature can read :-

您可以使用 .json 将 json 绑定到 POJO MappingHymansonHttpMessageConverter。因此,您的控制器签名可以读取:-

  public ResponseEntity<Boolean> saveData(@RequestBody RequestDTO req) 

Where RequestDTO needs to be a bean appropriately annotated to work with Hymanson serializing/deserializing. Your *-servlet.xml file should have the Hymanson message converter registered in RequestMappingHandler as follows :-

RequestDTO 需要是一个适当注释的 bean 以与 Hymanson 序列化/反序列化一起使用。您的 *-servlet.xml 文件应该在 RequestMappingHandler 中注册 Hymanson 消息转换器,如下所示:-

  <list >
    <bean class="org.springframework.http.converter.json.MappingHymansonHttpMessageConverter"/>

  </list>
</property>
</bean>

回答by Maheshbabu Jammula

In class do like this

在课堂上这样做

@RequestMapping(value = "/saveData", method = RequestMethod.POST)
 @ResponseBody
    public ResponseEntity<Boolean> saveData(HttpServletResponse response,Bean beanName) throws MyException {
        return new ResponseEntity<Boolean>(uiRequestProcessor.saveData(a),HttpStatus.OK);

}

In page do like this:

在页面中这样做:

<form enctype="multipart/form-data" action="<%=request.getContextPath()%>/saveData" method="post" name="saveForm" id="saveForm">
<input type="text" value="${beanName.userName }" id="username" name="userName" />

</from>

回答by HoldOffHunger

You will need these imports...

您将需要这些进口...

import javax.servlet.*;
import javax.servlet.http.*;

And, if you're using Maven, you'll also need this in the dependencies block of the pom.xml file in your project's base directory.

而且,如果您使用 Maven,您还需要在项目基本目录中 pom.xml 文件的依赖项块中使用它。

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

Then the above-listed fix by Jason will work:

然后上面列出的 Jason 修复将起作用:

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

回答by alowsarwar

You can get entire post body into a POJO. Following is something similar

您可以将整个帖子正文放入 POJO。以下是类似的东西

@RequestMapping(value = { "/api/pojo/edit" }, method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
@ResponseBody
public Boolean editWinner( @RequestBody Pojo pojo) { 

Where each field in Pojo (Including getter/setters) should match the Json request object that the controller receives..

Pojo 中的每个字段(包括 getter/setter)都应该匹配控制器接收到的 Json 请求对象。

回答by LambdaExpression

You can try using @RequestBodyParam

您可以尝试使用@RequestBodyParam

@RequestMapping(value = "/saveData", headers="Content-Type=application/json", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Boolean> saveData(@RequestBodyParam String source,@RequestBodyParam JsonDto json) throws MyException {
    ...
}

https://github.com/LambdaExpression/RequestBodyParam

https://github.com/LambdaExpression/RequestBodyParam