SSM报错:No converter found for return value of type: class java.util.ArrayList at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverter

我使用的是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

上一篇:IOS学习笔记06---C语言函数


下一篇:mvc:annotation-driven' must have no character or element问题