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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章