I am Using Spring WebServiceTemplate to make webservice call which uses JAXB to generate request XML. My requirement needs all the elements (including root) to have a namespace prefix (there is only a single namespace) in the SOAP request.
Ex :
<ns1:Login xmlns:ns1="www.example.com/a"> <ns1:username>abc</ns1:username> <ns1:password>abc</ns1:password> </ns1:Login>
But i am getting
<Login xmlns="www.example.com/a"> <username>abc<username> <password>abc<password> </Login>
xsd :
<?xml version="1.0" encoding="UTF-8"?> <xs:schema targetNamespace="www.example.com/a" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ilreq="www.example.com/a" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:complexType name="Login"> <xs:sequence> <xs:element name="username" type="xs:string"/> <xs:element name="password" type="xs:string"/> </xs:sequence> </xs:complexType>
Generated Java Class from XSD
@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "Login", propOrder = { "username", "password" }) @XmlRootElement public class Login { @XmlElement(required = true) protected String username; @XmlElement(required = true) protected String password; ...... }
package-info.java
@javax.xml.bind.annotation.XmlSchema( namespace = "www.example.com/a", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package authenticator.beans.login;
Want to know how to generate the request XML with Namespace prefix to all elements including root.
If you are using the default JAXB implementation provided with Java 6 or later, you can configure the namespace prefixes by extending the NamespacePrefixMapper and setting a property to tell the marshaller to use your extension.
You can use the NamespacePrefixMapper extension to control the namespace prefixes for your use case. The same extension is supported by both the JAXB reference implementation and EclipseLink JAXB (MOXy).
As a convenience, you can specify a prefix (an alias) for a namespace. A prefix is not an informational part of the element, attribute name, or namespace, and a prefix can be used with both element and attribute names. However, you can specify a default namespace and omit the prefix.
When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".
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
MSK,
Have you tried setting a namespace declaration to your member variables like this? :
@XmlElement(required = true, namespace = "http://example.com/a") protected String username; @XmlElement(required = true, namespace = "http://example.com/a") protected String password;
For our project, it solved namespace issues. We also had to create NameSpacePrefixMappers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With