SpringBoot项目Controller层返回对象提示异常:No converter found for return value of type【解决办法】

完整的异常信息如下:

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap

出现以上错误,原因在于,缺少了springmvc进行json转换需要的jar包,引入对应jar依赖:

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.6</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.9.6</version>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.6</version>
</dependency>

另外在pom文件中如果引入springboot-web依赖排除掉了springboot-web自带的json也会导致上面的异常信息:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
	  <artifactId>spring-boot-starter-json</artifactId>
	  <groupId>org.springframework.boot</groupId>
	</exclusion>
  </exclusions>
</dependency>

 此时,需要将依赖改为以下即可:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

发布了114 篇原创文章 · 获赞 77 · 访问量 24万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章