我有此请求
@RequestMapping(value = "/ecran", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Champ> getEcranChamps (@RequestParam("idType") Long idTypeDemande) {
System.err.println("La taille de la liste recupérer est " + champService.recupereListChampByIdtypeDemande(idTypeDemande).size());
return champService.recupereListChampByIdtypeDemande(idTypeDemande) ;
}
它返回一个List
,当我尝试使用这样的请求结果时:
$
.ajax({
type : "POST",
url : "/ecran",
data : {
idType : theme
},
success : function(result) {
var html = "";
for (var i = 0; i < result.length; i++) {
....
我遇到以下错误
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:未找到类org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer的序列化器,也未发现创建BeanSerializer的属性(为避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过参考链:java.util.ArrayList [0]-> com.kepler.portailclient.domain.model.Champ _ $$ _ jvst4aa_2a5 [“ handler”]) 在com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1191)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:312)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:71)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:33)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:145)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:107)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:400)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ObjectWriter $ Prefetch.serialize(ObjectWriter.java:1392)〜[jackson-databind-2.9.6.jar:2.9.6] 在com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:913)〜[jackson-databind-2.9.6.jar:2.9.6] 在org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:288)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.http.converter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:102)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:272)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)〜[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在javax.servlet.http.HttpServlet.service(HttpServlet.java:661)〜[tomcat-embed-core-8.5.32.jar:8.5.32] 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851)〜[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE] 在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)〜[tomcat-embed-core-8.5.32.jar:8.5.32]
答案 0 :(得分:0)
您遇到了JPA而不是REST的问题。
在JPA中,关系OneToMany具有两种行为LAZY和EAGER。
您可以在以下位置查看正确的解释:Difference between FetchType LAZY and EAGER in Java Persistence API?
当Jackson调用getOneToMany关系时,JPA试图调用对数据库的查询,因此引发LazyExecption。无论如何,我不建议直接返回JPA实体,因为它将依赖于循环依赖关系,最好使用要返回的信息创建DTO(Java Pojos)。因此,您必须: