在执行此操作时获得以下异常。
HandlerMethod details:
Controller [application.entry.controller.UserController]
Method [public java.lang.String application.entry.controller.UserController.handleSave(java.util.List<java.util.Map<java.lang.String, java.lang.String>>)]
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:124)[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:91)[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:71)[org.springframework.web.servlet-3.1.1.RELEASE.jar:3.1.1.RELEASE]
控制器类:
@RequestMapping(value="/save", method=RequestMethod.POST)
@ResponseBody public String handleSave(@RequestBody List<Map<String, String>> client)
{
Map<String, String> formInputs = new HashMap<String, String>();
for (Map<String, String> formInput : client) {
formInputs.put(formInput.get("name"), formInput.get("value"));
}
我也试过了@RequestMapping(value="/save", method=RequestMethod.POST, headers = "Accept=application/json")
,但错误是一样的。
弹簧servlet.xml中
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>
jQuery提交:
$('form').submit(function () {
alert();
$.ajax({
url: $(this).attr('action'),
type: 'POST',
data: JSON.stringify($(this).serializeArray()),
contentType: 'application/json',
success: function (data) {
alert('data')
},
error: function (jqXHR, textStatus, errorThrown) {
alert('An error has occured!! :-(')
}
})
return false
})
答案 0 :(得分:4)
尝试调整@RequestMapping
以接受JSON。
@RequestMapping(
value="/save",
method=RequestMethod.POST,
consumes="application/json")