我尝试从spring控制器查询客户列表作为json。当我访问网址时,我可以获得列表没有错误。
-CustomerController
@RequestMapping("getCustomerListRB")
public @ResponseBody List<Customer> getCustomerListsRB() {
List<Customer> cusList = new ArrayList<Customer>();
cusList = cusService.getList();
return cusList;
}
但是当我尝试在客户模型中加入表时 -Customer.java
@OneToMany(mappedBy = "Customer", cascade = CascadeType.ALL)
private List<MainOrder> MainOrder;
public List<MainOrder> getMainOrder() {
return MainOrder;
}
public void setMainOrder(List<MainOrder> mainOrder) {
MainOrder = mainOrder;
}
再次运行服务器它有错误如下:
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection of role: com.mdr.model.Customer.mainOrders, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.mdr.model.Customer["mainOrders"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.mdr.model.Customer.mainOrders, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->com.mdr.model.Customer["mainOrders"])
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal(MappingJackson2HttpMessageConverter.java:256)
org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:208)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:143)
org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:89)
org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:193)
org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:71)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
在我添加fetch = FetchType.EAGER后如下
@OneToMany(mappedBy = "Customer", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
我再次运行服务器它没有错误但无法获取数据。请帮帮我!
答案 0 :(得分:1)
首先,您需要一个持续时间足以使Session可用于延迟提取相关实体的事务。在您的示例中,您将返回在事务范围之外具有未获取的相关实体的实体列表(Spring internals将在事务结束后将List转换为JSON - 这就是它失败的原因。)
您可以执行以下操作之一:
答案 1 :(得分:1)
当使用带弹簧的杰克逊时我得到了同样的例外。将@jsonignore添加到声明为lazy的字段或在实体类中创建循环依赖。确保在应用程序的所有层中使用相同的库(codehaus / jackson / ..)jsonignore。
答案 2 :(得分:0)
看看: Spring MVC :could not initialize proxy - no Session (through reference chain:
作为解决方案,您可以使用附加实体注释来阻止延迟初始化
@Entity @Table(name="Country") **@Proxy(lazy = false)** public class Country {}
或在映射中使用@JsonBackReference