如果您想Content-type:application/x-www-form- urlencoded
表示发送到服务器的HTTP请求的主体应该是一个巨大的字符串- 名称/值对由&符分隔,(&)
并且urlencoded
就像他的名字所暗示的那样。
name=name1&value=value2
这意味着您不应使用,@RequestParam
因为参数是在http请求的正文中传递的。
因此,如果您想content-type
从他们的文档中使用它:
您可以使用HttpMessageConverter将请求主体转换为方法参数。HttpMessageConverter负责从HTTP请求消息转换为对象,并从对象转换为HTTP响应主体。RequestMappingHandlerAdapter通过以下默认HttpMessageConverters支持@RequestBody批注:
ByteArrayHttpMessageConverter转换字节数组。
StringHttpMessageConverter转换字符串。
FormHttpMessageConverter将表单数据与MultiValueMap之间进行转换。
SourceHttpMessageConverter可与javax.xml.transform.source之间进行转换。
您应该使用@RequestBody
with 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";
}