无法从Spring restcontroller发送实体对象作为JSON

时间:2017-09-06 12:11:52

标签: java json spring spring-mvc

我使用spring restcontroller将实体作为JSON对象发送。我已经添加了所有必要的jar

我的客户类

public class Customer  implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
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;
}


}

JARs添加

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版本)

我没有任何xml配置,它是纯粹的注释配置。我的代码是

的AppConfig

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "net.ifg.spring")
public class AppConfig {

}

AppInitializer

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class[] getRootConfigClasses() {
    return new Class[] { AppConfig.class };
}

@Override
protected Class[] getServletConfigClasses() {
    return null;
}

@Override
protected String[] getServletMappings() {
    return new String[] { "/" };
}

}

客户模型类

public class Customer  implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Long id;
private String firstName;


public Customer(long id, String firstName) {
    this.id = id;
    this.firstName = firstName;     
}

public Customer() {
}

// setters and getters of id and name

}

@RestController
public class CustomerRestController {

@Autowired
private CustomerDAO customerDAO;

@SuppressWarnings({ "rawtypes", "unchecked" })
@GetMapping("/customers/{id}")
public ResponseEntity getCustomer(@PathVariable("id") Long id) {
    System.out.println("Customer ID :"+id);         
    return new ResponseEntity(new Customer(101,"Aliya"), HttpStatus.OK);
}

}

发生异常是

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: No converter found for return value of type: class net.qiib.spring.model.Customer
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)

root cause

java.lang.IllegalArgumentException: No converter found for return value of type: class net.qiib.spring.model.Customer
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:187)
org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:203)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:113)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
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)

P.S:我没有使用任何XML配置,只使用了注释配置。

2 个答案:

答案 0 :(得分:2)

您可以使用SpringBoot。我很高兴您使用本指南https://spring.io/guides/gs/rest-service/

答案 1 :(得分:0)

Spring告诉你,它还没有发现任何将你的Java类转换为Json的东西。

看看如何使用杰克逊和这种注释:/orders/1 /orders/2013/06/16