JAXB :Need Namespace Prefix to all the elements
Solved by adding
@XmlSchema(
namespace = "http://www.example.com/a",
elementFormDefault = XmlNsForm.QUALIFIED,
xmlns = {
@XmlNs(prefix="ns1", namespaceURI="http://www.example.com/a")
}
)
package authenticator.beans.login;
import javax.xml.bind.annotation.*;
in package-info.java
Took help of jaxb-namespaces-missing : Answer provided by Blaise Doughan
Specifying prefix to namespace JAXB
As remarked in the comments, your @XmlSchema
lacks a namespace
attribute, therefore all @XmlRootElement
and @XmlType
are considered to be without a namespace, unless explicitly qualified. Your java-package.info
should look like this:
@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED, namespace = "http://foo/3.0/api", xmlns = {
@XmlNs(namespaceURI = "http://foo/3.0/api", prefix = ""),
@XmlNs(namespaceURI = "http://foo/3.0/base", prefix = "base"),
@XmlNs(namespaceURI = "http://foo/1.0/common", prefix = "common")})
package exp._3_0.api;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
The @XmlNs
annotation does not specify the default namespace used in the Java package. They only provide a suggestion to the JAXB implementation on what prefixes to assign to those namespaces. In your example the tags <TokenRequest>
, <software>
, <softwareId>
and <softwareName>
do not belong to any namespace, therefore JAXB uses an empty prefix for these elements and must use another prefix (ns4
) for the elements in the http://foo/3.0/api
namespace (there aren't any).
Is it possible to customize the namespace prefix that JAXB uses when marshalling to a String?
http://hwellmann.blogspot.com/2011/03/jaxb-marshalling-with-custom-namespace.html
This shows how to do it.
Another:
http://www.systemmobile.com/?p=280
Key bits in case that link dies too:
the NamespacePrefixMapper class, found in the com.sun.xml.bind.marshaller package. The abstract class has one method to implement:
public abstract String getPreferredPrefix(
String namespaceUri,
String suggestion,
boolean requirePrefix);
then
Marshaller marshaller =
jaxbContext.createMarshaller();
marshaller.setProperty(”com.sun.xml.bind.namespacePrefixMapper”,
new MyNamespacePrefixMapper());
If you’re also using javax.xml.xpath.XPath, your NamespacePrefixMapper can also implement javax.xml.namespace.NamespaceContext, centralizing your namespace customization in a single class.
JAXB marshal namespace prefix
Fixed by replacing
@XmlMixed
@XmlAnyElement(lax = true)
protected List<Object> content;
With the expected entity
@XmlElement(namespace="http://www.portalfiscal.inf.br/nfe")
protected TRetEnviNfe;
jaxb namespace gives unneeded ns2 prefix
The namespace prefix is needed, since you have elements like <subtag>
without a namespace. Once you assign a namespace to them, the ns2
prefix will disappear and JAXB will use a default namespace as your required.
There is also a comment in NamespaceContextImpl#declareNsUri
of JAXB RI that states:
// the default prefix is already taken.
// move that URI to another prefix, then assign "" to the default prefix.
Edit: If you cannot get rid of the unqualified elements, you can qualify them with the same namespace as the other elements. This is possible at least with JAXB RI and is equivalent to a <xsd:include>
tag:
final Map<String, String> props = Collections.singletonMap(JAXBRIContext.DEFAULT_NAMESPACE_REMAP, "http://namespace");
final JAXBContext ctx = JAXBContext.newInstance(new Class[]{Create.class}, props);
Related Topics
How to Load Files into My Java Application
Any Simple Way to Explain Why I Cannot Do List<Animal> Animals = New Arraylist<Dog>()
Convert Between Localdate and SQL.Date
Enable Partial Compilation in Intellij Idea
How to Use This Boolean in an If Statement
Steganography in Lossy Compression (Java)
Error:Java.Lang.Nosuchmethoderror: Org.Objectweb.Asm.Classwriter.<Init>(I)V
How to Create a Delay in Swing
Retrieving Servlet Context, Session and Request in a Pojo Outside Container
Random Number with Probabilities
Alternative to Deprecated Getcelltype
Using Variables Outside of an If-Statement
Jackson Change JSONignore Dynamically
Spring Boot Actuator Without Spring Boot
Calling Base Class Overridden Function from Base Class Method
Get Key Press Without Pressing Enter in Console
Modifier Static Is Only Allowed in Constant Variable Declarations