java Spring REST 控制器发布请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29423320/
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-11-02 15:14:43  来源:igfitidea点击:

Spring REST controller post request

javaspring-mvcservlets

提问by Steve

I have this controller in spring

我在春天有这个控制器

@RestController
public class GreetingController {

    @RequestMapping(value = "/greeting",  method = RequestMethod.POST)
    public String greeting(@RequestParam("uouo") String uouo) {
        return uouo;
    }
}

and when I testing it

当我测试它时

 curl -k -i -X POST -H "Content-Type:application/json"  -d uouo=test http://192.168.1.104:8080/api/greeting

the result of the testing

测试结果

HTTP Status 400 - Required String parameter 'uouo' is not present

HTTP 状态 400 - 所需的字符串参数“uouo”不存在

I tried may thing, but I think @RequestParamcan't use for POST it always passed the parameter in URL using GET, I use post only if I had object JSON as parameter using @RequestBody, is there any way to make string parameter send using POST?

我尝试过可能的事情,但我认为@RequestParam不能用于 POST 它总是使用 GET 在 URL 中传递参数,只有当我使用对象 JSON 作为参数时我才使用 post @RequestBody,有没有办法让字符串参数使用 POST 发送?

回答by Sotirios Delimanolis

The Servlet container will only provide parameters from the body for POST requests if the content type is application/x-www-form-urlencoded. It will ignore the body if the content type is anything else. This is specified in the Servlet Specification Chapter 3.1.1 When Parameters Are Available

如果内容类型为application/x-www-form-urlencoded. 如果内容类型是其他任何内容,它将忽略正文。这在Servlet 规范章节 3.1.1 当参数可用时指定

The following are the conditions that must be met before post form data will be populated to the parameter set:

  1. The request is an HTTP or HTTPS request.
  2. The HTTP method is POST.
  3. The content type is application/x-www-form-urlencoded.
  4. The servlet has made an initial call of any of the getParameterfamily of methods on the request object.

If the conditions are not met and the post form data is not included in the parameter set, the post data must still be available to the servlet via the request object's input stream. If the conditions are met, post form data will no longer be available for reading directly from the request object's input stream.

以下是在将发布表单数据填充到参数集之前必须满足的条件:

  1. 该请求是 HTTP 或 HTTPS 请求。
  2. HTTP 方法是 POST。
  3. 内容类型为application/x-www-form-urlencoded
  4. servlet 已getParameter对请求对象上的任何系列方法进行了初始调用。

如果不满足条件并且发布表单数据未包含在参数集中,则发布数据必须仍可通过请求对象的输入流提供给 servlet。如果满足条件,则不能再直接从请求对象的输入流中读取发布表单数据。

Since you aren't sending any JSON, just set the appropriate content type

由于您没有发送任何 JSON,只需设置适当的内容类型

curl -k -i -X POST -H "Content-Type:application/x-www-form-urlencoded" -d uouo=test http://192.168.1.104:8080/api/greeting

or let curlinfer it

或者让我们curl推断一下

curl -k -i -X POST -d uouo=test http://192.168.1.104:8080/api/greeting?uouo=test

Note that you can still pass query parameters in the URL

请注意,您仍然可以在 URL 中传递查询参数

curl -k -i -X POST -H "Content-Type:application/json"  http://192.168.1.104:8080/api/greeting?uouo=test