1. 程式人生 > >解決java.lang.IllegalArgumentException: No converter found for return value of type 的問題

解決java.lang.IllegalArgumentException: No converter found for return value of type 的問題

controller返回一個dto,並且定義了@ResponseBody註解,表示希望將這個dto物件轉換為json字串返回給前端,但是執行時報錯:nested exception is java.lang.IllegalArgumentException: No converter found for return value of type:XXX.XXX.dto。

這是因為springmvc預設是沒有物件轉換成json的轉換器的,需要手動新增jackson依賴。

解決方法為手動新增jackson依賴到pom.xml檔案中:

<properties>
    <
jackson.version>2.5.4</jackson.version> </properties> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency
> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency>

如果還是沒有解決,則在springmvc配置檔案中進行如下配置:

<mvc:annotation-driven>
     <mvc:message-converters
> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven>