Spring MVC - @RequestParam 使用 x-www-form-urlencoded 导致 MissingServletRequestParameterException
Spring MVC - @RequestParam causing MissingServletRequestParameterException with x-www-form-urlencoded
下面SpringMVC代码抛出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 的请求 returns 出现 HTTP 400 错误。
如果你想 Content-type:application/x-www-form-urlencoded
意味着发送到服务器的 HTTP 请求的主体应该是一个巨大的字符串——name/value 对由 & 符号分隔 (&)
和正如他的名字所暗示的那样,将是 urlencoded
。
name=name1&value=value2
这意味着您不应该使用 @RequestParam
,因为参数是在 http 请求的正文中传递的。
因此,如果您想使用他们文档中的 content-type
:
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 converts byte arrays.
StringHttpMessageConverter converts strings.
FormHttpMessageConverter converts form data to/from a MultiValueMap.
SourceHttpMessageConverter converts to/from a
javax.xml.transform.Source.
您应该将 @RequestBody
与 FormHttpMessageConverter
一起使用,这将得到这个巨大的字符串并将其转换为 MultiValueMap<String,String>
。这是一个示例。
@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";
}
下面SpringMVC代码抛出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 的请求 returns 出现 HTTP 400 错误。
如果你想 Content-type:application/x-www-form-urlencoded
意味着发送到服务器的 HTTP 请求的主体应该是一个巨大的字符串——name/value 对由 & 符号分隔 (&)
和正如他的名字所暗示的那样,将是 urlencoded
。
name=name1&value=value2
这意味着您不应该使用 @RequestParam
,因为参数是在 http 请求的正文中传递的。
因此,如果您想使用他们文档中的 content-type
:
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 converts byte arrays.
StringHttpMessageConverter converts strings.
FormHttpMessageConverter converts form data to/from a MultiValueMap.
SourceHttpMessageConverter converts to/from a javax.xml.transform.Source.
您应该将 @RequestBody
与 FormHttpMessageConverter
一起使用,这将得到这个巨大的字符串并将其转换为 MultiValueMap<String,String>
。这是一个示例。
@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";
}