Spring:没有用于预设内容类型为“null”的[类java.util.ArrayList]的转换器

dced5bon  于 2022-11-21  发布在  Spring
关注(0)|答案(1)|浏览(255)

我不断得到这个错误,即使明显的可能原因似乎得到照顾(在调查类上缺少公共getter,从依赖项中缺少Jackson...)

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.util.ArrayList] with preset Content-Type 'null'] with root cause

org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.util.ArrayList] with preset Content-Type 'null'

控制器不工作:

@RestController
@RequestMapping("/api")
public class SurveyController {

    @Autowired
    SurveyService surveyService;

    @RequestMapping(value = "/surveys", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody List<Survey> getAllSurveys() {
        List<Survey> surveys = surveyService.findAll();

        return surveys; // <-- ERROR HERE
    }
}

公共getter和setter是在以下情况下生成的:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Survey {
    private String sid;
    private String surveyls_title;
    @Nullable
    private String startdate;
    @Nullable
    private String expires;
    private String active;
}

服务按预期运行,返回调查列表
第一个
POM.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.6</version>
  </parent>

  <properties>
    <java.version>17</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>2.1.210</version>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

依赖关系树显示存在jackson-databind和jackson-core

[INFO] --- maven-dependency-plugin:3.1.2:tree (default-cli) @ SurveyAPI ---
[INFO] com.**********:SurveyAPI:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.5.6:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.5.6:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.5.6:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.5.6:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.5.6:compile
[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.6:compile
[INFO] |  |  |  |  +- ch.qos.logback:logback-core:jar:1.2.6:compile
[INFO] |  |  |  |  \- org.slf4j:slf4j-api:jar:1.7.32:compile
[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.14.1:compile
[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.14.1:compile
[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.32:compile
[INFO] |  |  +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] |  |  +- org.springframework:spring-core:jar:5.3.12:compile
[INFO] |  |  |  \- org.springframework:spring-jcl:jar:5.3.12:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.28:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.5.6:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.12.5:compile
[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.12.5:compile
[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.12.5:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.12.5:compile
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.12.5:compile
[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.12.5:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.5.6:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.54:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.54:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.54:compile
[INFO] |  +- org.springframework:spring-web:jar:5.3.12:compile
[INFO] |  |  \- org.springframework:spring-beans:jar:5.3.12:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:5.3.12:compile
[INFO] |     +- org.springframework:spring-aop:jar:5.3.12:compile
[INFO] |     +- org.springframework:spring-context:jar:5.3.12:compile
[INFO] |     \- org.springframework:spring-expression:jar:5.3.12:compile
[INFO] +- org.springframework.boot:spring-boot-starter-webflux:jar:2.5.6:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-reactor-netty:jar:2.5.6:compile
[INFO] |  |  \- io.projectreactor.netty:reactor-netty-http:jar:1.0.12:compile
[INFO] |  |     +- io.netty:netty-codec-http:jar:4.1.69.Final:compile
[INFO] |  |     |  +- io.netty:netty-common:jar:4.1.69.Final:compile
[INFO] |  |     |  +- io.netty:netty-buffer:jar:4.1.69.Final:compile
[INFO] |  |     |  +- io.netty:netty-transport:jar:4.1.69.Final:compile
[INFO] |  |     |  +- io.netty:netty-codec:jar:4.1.69.Final:compile
[INFO] |  |     |  \- io.netty:netty-handler:jar:4.1.69.Final:compile
[INFO] |  |     +- io.netty:netty-codec-http2:jar:4.1.69.Final:compile
[INFO] |  |     +- io.netty:netty-resolver-dns:jar:4.1.69.Final:compile
[INFO] |  |     |  +- io.netty:netty-resolver:jar:4.1.69.Final:compile
[INFO] |  |     |  \- io.netty:netty-codec-dns:jar:4.1.69.Final:compile
[INFO] |  |     +- io.netty:netty-resolver-dns-native-macos:jar:osx-x86_64:4.1.69.Final:compile
[INFO] |  |     |  \- io.netty:netty-transport-native-unix-common:jar:4.1.69.Final:compile
[INFO] |  |     +- io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.69.Final:compile
[INFO] |  |     \- io.projectreactor.netty:reactor-netty-core:jar:1.0.12:compile
[INFO] |  |        \- io.netty:netty-handler-proxy:jar:4.1.69.Final:compile
[INFO] |  |           \- io.netty:netty-codec-socks:jar:4.1.69.Final:compile
[INFO] |  \- org.springframework:spring-webflux:jar:5.3.12:compile
[INFO] |     \- io.projectreactor:reactor-core:jar:3.4.11:compile
[INFO] |        \- org.reactivestreams:reactive-streams:jar:1.0.3:compile
[INFO] +- com.h2database:h2:jar:2.1.210:compile
[INFO] \- org.projectlombok:lombok:jar:1.18.22:provided

有人知道我做错了什么吗?谢谢

w9apscun

w9apscun1#

自己也发现了问题:我试图将WebFlux和非React式Spring结合使用。

@EnableAutoConfiguration(exclude = {WebMvcAutoConfiguration.class })

到主类,这解决了 Boot 期间的异常:

Invalid bean definition with name 'requestMappingHandlerAdapter' defined in class path resource [...]: Cannot register bean definition [...] for bean 'requestMappingHandlerAdapter': There is already [...] bound.

但可能会禁用某些Jackson组件,从而导致我遇到的问题。
通过返回Flux<Survey>而不是List<Survey>,将SurveyController转换为React式SurveyController来解决

相关问题