Spring boot 没有适合内容类型应用程序/json的HttpMessageConverter;字符集=UTF-8

Spring boot 没有适合内容类型应用程序/json的HttpMessageConverter;字符集=UTF-8,spring-boot,jackson,resttemplate,Spring Boot,Jackson,Resttemplate,我在标题中得到了一个例外,即使在添加了在其他解决方案中在线找到的所有代码之后(我添加了HttpMessageConverters以及APPLICATION\u JSONaccept头) 有人知道吗 编辑:pom.xml中的相关依赖项 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa<

我在标题中得到了一个例外,即使在添加了在其他解决方案中在线找到的所有代码之后(我添加了
HttpMessageConverters
以及
APPLICATION\u JSON
accept头)

有人知道吗

编辑:pom.xml中的相关依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-integration</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.8</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>

org.springframework.boot
spring引导启动器数据jpa
org.springframework.boot
spring boot启动器集成
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动装置
org.springframework.boot
弹簧起动试验
测试
com.fasterxml.jackson.core
杰克逊核心
2.9.8
com.fasterxml.jackson.core
杰克逊数据绑定
2.9.8

请注意,要使HttpMessageConverter符合映射条件,它的canRead方法必须返回true

对于AbstractJackson2HttpMessageConverter(MappingJackson2HttpMessageConverter的父级),它不仅检查MediaType,还检查Jackson是否可以反序列化对象

@Override
public boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType) {
    if (!canRead(mediaType)) {
        return false;
    }
    JavaType javaType = getJavaType(type, contextClass);
    AtomicReference<Throwable> causeRef = new AtomicReference<>();
    if (this.objectMapper.canDeserialize(javaType, causeRef)) {
        return true;
    }
    logWarningIfNecessary(javaType, causeRef.get());
    return false;
}
@覆盖
公共布尔值可读取(类型类型,@Nullable Class contextClass,@Nullable MediaType MediaType){
如果(!canRead(mediaType)){
返回false;
}
JavaType JavaType=getJavaType(类型,contextClass);
AtomicReference causeRef=新的AtomicReference();
if(this.objectMapper.canDeserialize(javaType,causeRef)){
返回true;
}
logwarningifrequired(javaType,causeRef.get());
返回false;
}
现在,我相信您的
JsonCreator
是不正确的

注意:注释创建者方法(构造函数、工厂方法)时,方法必须为:

  • 单参数构造函数/工厂方法,参数不带JsonProperty注释:若有,则称为“委托创建者”,在这种情况下,Jackson首先将JSON绑定到参数类型中,然后调用创建者。这通常与JsonValue(用于序列化)结合使用
  • 构造函数/工厂方法,其中每个参数都用JsonProperty或JacksonInject注释,以指示要绑定到的属性的名称

另请参见

注意,要使HttpMessageConverter符合映射条件,其canRead方法必须返回true

对于AbstractJackson2HttpMessageConverter(MappingJackson2HttpMessageConverter的父级),它不仅检查MediaType,还检查Jackson是否可以反序列化对象

@Override
public boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType) {
    if (!canRead(mediaType)) {
        return false;
    }
    JavaType javaType = getJavaType(type, contextClass);
    AtomicReference<Throwable> causeRef = new AtomicReference<>();
    if (this.objectMapper.canDeserialize(javaType, causeRef)) {
        return true;
    }
    logWarningIfNecessary(javaType, causeRef.get());
    return false;
}
@覆盖
公共布尔值可读取(类型类型,@Nullable Class contextClass,@Nullable MediaType MediaType){
如果(!canRead(mediaType)){
返回false;
}
JavaType JavaType=getJavaType(类型,contextClass);
AtomicReference causeRef=新的AtomicReference();
if(this.objectMapper.canDeserialize(javaType,causeRef)){
返回true;
}
logwarningifrequired(javaType,causeRef.get());
返回false;
}
现在,我相信您的
JsonCreator
是不正确的

注意:注释创建者方法(构造函数、工厂方法)时,方法必须为:

  • 单参数构造函数/工厂方法,参数不带JsonProperty注释:若有,则称为“委托创建者”,在这种情况下,Jackson首先将JSON绑定到参数类型中,然后调用创建者。这通常与JsonValue(用于序列化)结合使用
  • 构造函数/工厂方法,其中每个参数都用JsonProperty或JacksonInject注释,以指示要绑定到的属性的名称

另请参见默认情况下spring或springboot在启动期间配置以下消息转换器:

ByteArrayHttpMessageConverter–转换字节数组
StringHttpMessageConverter–转换字符串
ResourceHttpMessageConverter–为任何类型的八位字节流转换org.springframework.core.io.Resource
SourceHttpMessageConverter–转换javax.xml.transform.Source
FormHttpMessageConverter–将表单数据与多值映射进行转换。
Jaxb2RootElementHttpMessageConverter–将Java对象转换为XML或从XML转换为Java对象
MappingJackson2HttpMessageConverter–转换JSON
MappingJacksonHttpMessageConverter–转换JSON
AtomFeedHttpMessageConverter–转换Atom提要
RssChannelHttpMessageConverter–转换RSS源
但是对于要添加的Jackson转换器,spring必须检测类路径中是否存在Jackson,因此通过向应用程序添加Jackson依赖项,转换器应该自动配置,除非您使用
@EnableWebMVC
注释显式阻止自动配置

另外,确保如果您使用的是rest端点,那么该方法的注释是正确的,即对类使用
@RestController
,否则您必须提供
@ResponseBody
注释以指示spring是rest端点

从文件中:

指示方法参数的注释应绑定到web请求的主体。请求的主体通过 HttpMessageConverter根据 请求的内容类型。也可以选择自动验证 通过使用
@Valid
注释参数来应用。支持 Servlet环境中带注释的处理程序方法


默认情况下,spring或springboot在启动期间配置以下消息转换器:

ByteArrayHttpMessageConverter–转换字节数组
StringHttpMessageConverter–转换字符串
ResourceHttpMessageConverter–为任何类型的八位字节流转换org.springframework.core.io.Resource
SourceHttpMessageConverter–转换javax.xml.transform.Source
FormHttpMessageConverter–将表单数据与多值映射进行转换。
Jaxb2RootElementHttpMessageConverter–转换
@Override
public boolean canRead(Type type, @Nullable Class<?> contextClass, @Nullable MediaType mediaType) {
    if (!canRead(mediaType)) {
        return false;
    }
    JavaType javaType = getJavaType(type, contextClass);
    AtomicReference<Throwable> causeRef = new AtomicReference<>();
    if (this.objectMapper.canDeserialize(javaType, causeRef)) {
        return true;
    }
    logWarningIfNecessary(javaType, causeRef.get());
    return false;
}
ByteArrayHttpMessageConverter – converts byte arrays
StringHttpMessageConverter – converts Strings
ResourceHttpMessageConverter – converts org.springframework.core.io.Resource for any type of octet stream
SourceHttpMessageConverter – converts javax.xml.transform.Source
FormHttpMessageConverter – converts form data to/from a MultiValueMap<String, String>.
Jaxb2RootElementHttpMessageConverter – converts Java objects to/from XML
MappingJackson2HttpMessageConverter – converts JSON
MappingJacksonHttpMessageConverter – converts JSON
AtomFeedHttpMessageConverter – converts Atom feeds 
RssChannelHttpMessageConverter – converts RSS feeds