gpt4 book ai didi

java - 不支持 Spring + Jackson 内容类型 'application/json;charset=UTF-8'

转载 作者:行者123 更新时间:2023-11-30 10:27:23 25 4
gpt4 key购买 nike

当使用 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;

抛出异常。这是我的代码;

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
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>

最佳答案

我遇到了同样的问题,我通过向我的类添加默认构造函数解决了这个问题。试试吧。

关于java - 不支持 Spring + Jackson 内容类型 'application/json;charset=UTF-8',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45324135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com