How to set default namespace using JAXB

I have an ATOM-XML view of my data that is returned through the Spring MVC web service. I use JAXB for serialization, I have several namespaces, but I want the default namespace to be set to Atom without a prefix. Here is what I still have in package-info.java , but the atom prefix is โ€‹โ€‹set to ns3.

 @XmlSchema(namespace = com.mycomponay.foo.ATOM_NAMESPACE, xmlns = { @XmlNs(prefix = "foo", namespaceURI = com.mycomponay.foo.NAMESPACE_FOO), }, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package com.mycompany.web; import javax.xml.bind.annotation.XmlNs; 

I also noticed that namespaces are displayed in chrome, but not in Firefox.

+4
source share
2 answers

Try adding the @XmlNs annotation with the prefix "" for the namespace that you want to display as the default.

 @XmlSchema( namespace = com.mycompany.foo.ATOM_NAMESPACE, xmlns = { @XmlNs(prefix = "", namespaceURI = com.mycompany.foo.ATOM_NAMESPACE), @XmlNs(prefix = "foo", namespaceURI = com.mycompany.foo.NAMESPACE_FOO) }, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package com.mycompany.web; import javax.xml.bind.annotation.*; 

Note:

The namespaces specified in the @XmlSchema annotation @XmlSchema intended to influence the generation of XML schemas and are not guaranteed to be used when the object model is converted to XML. However, EclipseLink JAXB (MOXy) and the latest versions of the JAXB Reference Implementation will use them whenever possible.

Additional Information

+6
source

if you use a separate class for the XML element, annotate it with namespace = "" will work.

+1
source

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


All Articles