Spring MVC - @RequestParam 导致 MissingServletRequestParameterException 与 x-www-form-urlencoded [英] Spring MVC - @RequestParam causing MissingServletRequestParameterException with x-www-form-urlencoded

查看:26
本文介绍了Spring MVC - @RequestParam 导致 MissingServletRequestParameterException 与 x-www-form-urlencoded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 Spring MVC 代码抛出 MissingServletRequestParameterException,

The follwing Spring MVC code throws MissingServletRequestParameterException,

@Controller
@RequestMapping("/users")
public class XXXXResource extends AbstractResource {

.....

 @RequestMapping(method = RequestMethod.PUT
            , produces = {"application/json", "application/xml"}
            , consumes = {"application/x-www-form-urlencoded"}
    )
    public
    @ResponseBody
    Representation createXXXX(@NotNull @RequestParam("paramA") String paramA,
        @NotNull @RequestParam("paramB") String paramB,
        @NotNull @RequestParam("paramC") String paramC,
        @NotNull @RequestParam("paramD") String paramD ) throws Exception {
   ...
   }
}

日志中没有堆栈跟踪,只有来自 Postman 的请求返回 HTTP 400 错误.

There are no stack traces in the logs, only the Request from Postman returns with HTTP 400 Error.

推荐答案

如果要Content-type:application/x-www-form-urlencoded 表示发送的 HTTP 请求的正文到服务器应该是一个巨大的字符串——名称/值对由与号 (&) 分隔,并且正如他的名字所暗示的那样将被 urlencoded .

if you want to Content-type:application/x-www-form-urlencoded means that the body of the HTTP request sent to the server should be one giant string -- name/value pairs are separated by the ampersand (&) and will be urlencoded as his name implies.

name=name1&value=value2

这意味着您不应该使用 @RequestParam 因为参数是在 http 请求的正文中传递的.

this means that you should not user @RequestParam because the arguments are passed in the body of the http request.

因此,如果您想从他们的文档中使用此 content-type:

So if you want to use this content-type from their doc:

您可以通过使用HttpMessageConverter.HttpMessageConverter 负责从 HTTP 请求消息转换为对象并转换从对象到 HTTP 响应正文.该RequestMappingHandlerAdapter 支持 @RequestBody 注解以下默认 HttpMessageConverters:

You convert the request body to the method argument by using an HttpMessageConverter. HttpMessageConverter is responsible for converting from the HTTP request message to an object and converting from an object to the HTTP response body. The RequestMappingHandlerAdapter supports the @RequestBody annotation with the following default HttpMessageConverters:

ByteArrayHttpMessageConverter 转换字节数组.

ByteArrayHttpMessageConverter converts byte arrays.

StringHttpMessageConverter 转换字符串.

StringHttpMessageConverter converts strings.

FormHttpMessageConverter 将表单数据与 MultiValueMap 相互转换.

FormHttpMessageConverter converts form data to/from a MultiValueMap.

SourceHttpMessageConverter 转换为/从javax.xml.transform.Source.

SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.

您应该将@RequestBodyFormHttpMessageConverter 一起使用,这将获得这个巨大的字符串并将其转换为MultiValueMap.这是一个示例.

You should use @RequestBody with FormHttpMessageConverter, which will get this giant string and will convert it to MultiValueMap<String,String>. Here is a sample.

@RequestMapping(method = RequestMethod.PUT
        , consumes = {"application/x-www-form-urlencoded"}
        ,value = "/choice"
)
public
@ResponseBody
String createXXXX(@RequestBody MultiValueMap params) throws Exception {
    System.out.println("params are " + params);
    return "hello";
}

这篇关于Spring MVC - @RequestParam 导致 MissingServletRequestParameterException 与 x-www-form-urlencoded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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