6

I'm struggling with the way how Moxy handles inheritance of objects.

In particular, I need to rename the default type element which Moxy adds in case of subtypes as it prevents me from having my own type field in my objects.

This question relates to the Remove "type" from JSON output jersey moxy but unfortunately, it doesn't answer my question.

I have tried to include @XmlDiscriminatorNode on my abstract class which didn't seem to make any difference in the resulting json at all.

I have also tried to remove the default moxy type element completely but without any success.

Community
  • 1
  • 1
Stepan Vavra
  • 3,884
  • 5
  • 29
  • 40
  • check http://stackoverflow.com/questions/13831189/xmldiscriminatornode-xmldescriminatorvalue-not-working-on-weblogic-server/13838091#13838091 – Hein Blöd Apr 06 '15 at 14:16
  • It seems Blaise is able to rename it to `classifier` attribute for XML, so I'll try to start over by using his example from his blog http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-moxy-extension.html. Thanks! – Stepan Vavra Apr 06 '15 at 14:28

1 Answers1

3

There has been change in handling of type property in MOXy 2.6. As of MOXy 2.6, type property is by default prefixed with xsi prefix (or whatever prefix you define). It means that there should be no type property clash in MOXy beginning with version 2.6.

Details can be found at https://wiki.eclipse.org/EclipseLink/DesignDocs/459464

Namespace prefix needs to be specified as JAXBContext property:

unmarshaller.setProperty(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON); Map namespaces = new HashMap<>(); namespaces.put(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "xsi"); unmarshaller.setProperty(JAXBContextProperties.NAMESPACE_PREFIX_MAPPER, namespaces);

Martin Vojtek
  • 391
  • 3
  • 7
  • With the added namespace prefix mapper, MOXy starts to put namespaces everywhere, which is something our Javascript clients do not accept. I'll see what I can do about eliminating a use of namespaces except for the `XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI` namespace... – Stepan Vavra Apr 10 '15 at 20:40
  • By the way, is it supported to rename the `type` by using `@XmlCustomizer` with a `DescriptorCustomizer` where one can call `ClassDescriptor.getInheritancePolicy().setClassIndicatorFieldName("@moxy-type")`? When I try to do this, MOXy looses it's capability to properly unmarshal JSON (sounds like a bug to me). – Stepan Vavra Apr 10 '15 at 20:47
  • Removing unnecessary namespaces should resolve the issue. I know that in some cases it could be really annoying. – Martin Vojtek Apr 11 '15 at 06:06
  • I have not used setClassIndicatorFieldName before. However, I have found this in moxy tests: NamespaceResolver nsResolver = new NamespaceResolver(); nsResolver.put("xsi", javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI); xmlDescriptor.setNamespaceResolver(nsResolver); xmlDescriptor.getInheritancePolicy().setClassIndicatorFieldName("@xsi:type"); Maybe you can use xmlDescriptor.getInheritancePolicy().setClassIndicatorFieldName("@type"); without namespaces. – Martin Vojtek Apr 11 '15 at 06:13