Reputation: 667
When posting with jQuery ajax, I get following error.
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)
The interesting thing is when I debug
RequestResponseBodyMethodProcessor.java:148
and get the webRequest.getHeader("Content-Type")
the value is application/json
. Which is pretty normal.
But when it comes to the last method of the chain AbstractMessageConverterMethodArgumentResolver.java:226
Content-Type becomes application/json; charset=utf-8;
Which throws an exception. Here is my code ;
Controller
@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
@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 Config
<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>
Upvotes: 2
Views: 5952
Reputation: 61
I had the same problem and I solved it adding a default constructor to my class. Try it.
Upvotes: 2