使用jQuery ajax发布时,我收到以下错误。
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:226)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:148)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:125)
有趣的是我调试时
RequestResponseBodyMethodProcessor.java:148
并获取webRequest.getHeader("Content-Type")
值为application/json
。这很正常。
但是当谈到链的最后一个方法AbstractMessageConverterMethodArgumentResolver.java:226
时,Content-Type变为application/json; charset=utf-8;
引发异常。这是我的代码;
控制器
@RequestMapping(value="/user/set-preference",method=RequestMethod.POST)
@ResponseBody
private boolean set_preference(@RequestBody UserPreference preference)
{
try{
userService.save(preference);
return true;
}catch(Exception ex){
return false;
}
}
实体
@Entity
public class UserPreference extends BaseEntity{
@ManyToOne(fetch=FetchType.LAZY)
private User user;
private String name;
private String information;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInformation() {
return information;
}
public void setInformation(String information) {
this.information = information;
}
}
Spring MVC配置
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean
class="com.test.HibernateAwareObjectMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
答案 0 :(得分:1)
我有同样的问题,我解决了它为我的类添加一个默认构造函数。试试吧。