gpt4 book ai didi

java - 在 Json Resteasy 上处理集合(多对多)的递归引用

转载 作者:行者123 更新时间:2023-12-02 04:58:03 25 4
gpt4 key购买 nike

我遇到了一个无法解决的问题。我有 2 个 DTO,其中一个具有另一个 DTO 的集合属性。

public class UnidadAsistencialDTO 

private Long id;
private String descripcion;
@JsonManagedReference
private Collection<ServicioDTO> servicios;


public class ServicioDTO

private Long id;
private String nombre;
private Collection<EspecialidadMinDTO> especialidades;
@JsonBackReference
private Collection<UnidadAsistencialDTO> unidadesAsistenciales;

所以当我尝试获取这些对象时,我遇到了这个异常

11:52:46,708 WARN [org.jboss.resteasy.core.SynchronousDispatcher] (http-localhost/127.0.0.1:8080-1) Failed executing POST /agenda: org.jboss.resteasy.spi.ReaderException: org.codehaus.jackson.map.JsonMappingException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.Collection) not compatible with managed type (ar.org.inssjp.asthenis.dto.agenda.UnidadAsistencialDTO) at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:202) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:136) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:159) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:231) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19] at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19] at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:926) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4] at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_71] Caused by: org.codehaus.jackson.map.JsonMappingException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.Collection) not compatible with managed type (ar.org.inssjp.asthenis.dto.agenda.UnidadAsistencialDTO) at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:315) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:620) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:379) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:407) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:352) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findTypedValueDeserializer(StdDeserializerProvider.java:180) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.ObjectMapper._findRootDeserializer(ObjectMapper.java:2829) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2699) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419) at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:106) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:63) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:109) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:169) [resteasy-jaxrs-2.3.8.Final-redhat-3.jar:] ... 27 more Caused by: java.lang.IllegalArgumentException: Can not handle managed/back reference 'defaultReference': back reference type (java.util.Collection) not compatible with managed type (ar.org.inssjp.asthenis.dto.agenda.UnidadAsistencialDTO) at org.codehaus.jackson.map.deser.BeanDeserializer._resolveManagedReferenceProperty(BeanDeserializer.java:497) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:382) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:407) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:352) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:620) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:379) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:407) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:352) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:620) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:379) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:407) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:352) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.BasicDeserializerFactory.createCollectionDeserializer(BasicDeserializerFactory.java:283) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createDeserializer(StdDeserializerProvider.java:389) [jackson-mapper-asl.jar:1.9.13] at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:310) [jackson-mapper-asl.jar:1.9.13] ... 44 more

我在一些博客中读到,当双方都有集合时(就像多对多关系),这些注释“@JsonManagedReference”和“@JsonBackReference”并不能解决问题。

你能帮我吗?

谢谢

最佳答案

尝试将此注释添加到类中:

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = UnidadAsistencialDTO.class))

还有

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = ServicioDTO.class))

还可以看看this以供进一步引用。

编辑

如果这不起作用(如您的情况),请尝试以下操作:

@JsonIgnore

此文档为 here .

更新

如果您需要 parent 及其 child 的列表并且 child 引用 parent ,我建议您采用以下任一方式:

  1. 在调用父级时删除子级中父级的引用,如果调用子级则删除子级的引用(仅在内存中且在将 DTO 转换为 json 之前)。你最终会得到这样的结果:

    { “姓名”:“ parent ”, “ children ”:[ { “姓名”:“ child 1”, “父”:空 },{ “姓名”:“ child 2”, “父”:空 } ]}

    子级对父级的引用中没有null。另外,作为提示,请确保不要删除事务上下文内部的父引用。

  2. 如果子级(或父级,取决于您要查询的内容)内部的 null 引用不正确,您可以创建一组映射到该结构的新 DTO您正在寻找。如果您愿意,请查看 this了解正确的方法。

关于java - 在 Json Resteasy 上处理集合(多对多)的递归引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28586838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com