Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename "type" from JSON moxy output

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.

like image 701
Stepan Vavra Avatar asked Apr 06 '15 14:04

Stepan Vavra


1 Answers

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);

like image 79
Martin Vojtek Avatar answered Nov 04 '22 01:11

Martin Vojtek