springMVC報java.lang.IllegalArgumentException: No converter found for return value of type異常
阿新 • • 發佈:2018-11-20
問題描述:使用springMVC框架,用@responseBody返回物件的json資料,報出此異常。
部分程式碼:
@ResponseBody
public Result query(HttpServletRequest request, HttpServletResponse response) {
return new Result();
}
原因:springMVC預設是沒有json物件的轉換器的,需要手動新增json轉換依賴。
解決方案:
手動新增jackson依賴:
<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>