1. 程式人生 > >SSM報錯:No converter found for return value of type: class java.util.ArrayList at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageC

SSM報錯:No converter found for return value of type: class java.util.ArrayList at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageC

我使用的是SSM框架,是在編寫測試RESTFUL介面的時候出現,

@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<List<User>> selectAll() {
        List<User> users = this.userService.selectAll();
        if (null != users && users.size() > 0) {
            
return ResponseEntity.ok(users); } return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null); }

程式碼不是非常嚴謹,只是做測試。

訪問:http://localhost:8081/selectAll

錯誤:

No converter found for return value of type: class java.util.ArrayList at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:187) at org.s

 

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

解決:新增相關依賴

<!--jacson支援json-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.4</version>
        </dependency>

成功解決:

[{"username":"admin","password":"admin"},{"username":"zhanxuewei","password":"123456"}]

參考:https://www.cnblogs.com/hafiz/p/5812873.html