Unmarshalling JAXB collection when using IDResolver, because IDResolver Object as the target type

I have this problem trying to cancel json from rest webservice (cxf). I am using JAXB and EclipseLink.

The object is displayed as follows:

@Entity @Table(name = "service_pkg_service", schema = "MD") @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class ServicePkgService extends DatabaseModel implements java.io.Serializable { @Transient @XmlIDREF private Set<ChannelPkgService> channelPkgServices = new HashSet<ChannelPkgService>(); } @Entity @Table(name = "channel_pkg_service", schema = "MD") @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement public class ChannelPkgService extends DatabaseModel implements java.io.Serializable{ @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "CHANNEL_PKG_ID") @XmlID @XmlAttribute private String id; } 

I have a class extending IDResolver, so I can create an object based on its identifier.

 public class EntityIDResolver extends IDResolver{ @Override public void bind(String id, Object obj) throws SAXException { } @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Callable<?> resolve(final String id, Class targetType) throws SAXException { } } 

I have a unmarshalling json problem like this "channelPkgService": [1,2,3], targetType class is java.lang.Object

I read this https://github.com/javaee/jaxb-v2/issues/546 and created a wrapper to handle this.

 public class ChannelPkgServiceWrapper extends HashSet<ChannelPkgService>{ } 

Sinse I have many such cases, and I do not want to create many shells, is there a more general way to handle this?

Forget about the versions used:

  • cxf.version: 2.3.6
  • EclipseLink: 2.3.0
  • JAXB-OS-2.1.13.jar (a jar containing Lister.class that does the actual work to get the correct type.)
+6
source share
1 answer

@XmlIDREF used by JAXB to map links within a document. Each object referenced by the identifier must also be embedded in an XML or JSON document:

If you want to marshal an object as its identifier, you need to use the XmlAdapter . See my answer to a similar question:

Also note that JAXB is a specification ( JSR-222 ) and EclipseLink contains MOXy (I am MOXy tech lead). This means that you can remove jaxb-impl-2.1.13.jar from your dependencies:

+1
source

Source: https://habr.com/ru/post/897646/


All Articles