我尝试将客户列表作为JSON响应发送,但是,它以错误响应返回
No converter found for return value of type: class java.util.ArrayList
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.ifg.spring")
public class AppConfig {
@Bean
public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
return builder;
}
}
我的控制器是
@RestController
public class CustomerRestController {
@Autowired
private CustomerDAO customerDAO;
@GetMapping("/customers")
public List<Customer> getCustomers() {
return customerDAO.list();
}
}
public class Customer {
private Long id;
private String firstName;
public Customer(long id, String firstName) {
this.id = id;
this.firstName = firstName;
}
public Customer() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
commons-logging-1.2.jar,jackson-core-asl-1.9.13.jar,jackson-mapper-asl-1.9.13.jar,jackson-databind-2.9.0.jar,spring-aop,spring -core,spring-beans,spring-context,spring-expression,spring-web,spring-webmvc(全4.3.9版本)
仍在回复我的错误回复。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
非常感谢任何想法
答案 0 :(得分:0)
在项目中添加Jackson依赖项或jar。
像,
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>VERSION</version></dependency>
答案 1 :(得分:-1)
如果在Spring MVC中进行此操作,则dispatcher-servlet的xml文件必须具有<mvc:annotation-driven />
标记才能进行转换。