70

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.

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
Sai Kumar
  • 2,231
  • 3
  • 17
  • 15
  • 4
    Correct me if I'm wrong but aren't these two XML snippets at the beginning equivalent? – Tomasz Nurkiewicz Aug 01 '11 at 07:51
  • 1
    @TomasZ : XML's are equivalent but the server is not accepting the request without the namespace prefix. So i want to have prefix to all elements. – Sai Kumar Aug 01 '11 at 07:57
  • 6
    Related question? [JAXB namespaces missing](http://stackoverflow.com/questions/6294065/jaxb-namespaces-missing) – perp Aug 01 '11 at 08:03
  • @perp : Thanks for the link. Able to generate the XML in desired format. – Sai Kumar Aug 01 '11 at 09:53
  • 5
    @MSK please 'close' this question per guidelines given in http://meta.stackexchange.com/questions/38149/should-this-question-be-closed-deleted-self-answered-or-what – Steen Aug 09 '11 at 07:51
  • I don't want it as nameSpace. Is it possible to have prefix like LOGIN_ for all xml elements under root Login – Akhil Surapuram Feb 10 '20 at 15:53

6 Answers6

100

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

Community
  • 1
  • 1
Sai Kumar
  • 2,231
  • 3
  • 17
  • 15
  • 1
    How can I define more than one namespaces? Like in the above example I want to add one more `namespace` url with it's `prefix`? – Dixit Singla Apr 28 '17 at 13:54
  • @sai I am following same approach with java6 cxf webservices. But I still don't see proper prefix. In my case, I am getting prefix 'a' instead of ns2. Any ideas? – San May 09 '18 at 04:14
  • @Sai can you please write code(the content of package-info.java and Generated Java Class from XSD) for SOAP request in which two(2) namespace prefixes are being used. It means for some element namespace prefix is ns1 and for some one ns2 – PRAFUL ANAND May 26 '19 at 16:53
10

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.

Pat B
  • 1,915
  • 23
  • 40
  • 1
    I did not use the NameSpacePrefixMappers.I had to go only with annotation approach in package-info.java. The Link provided by perp did the trick [i.e. reply from Blaise Doughan](http://stackoverflow.com/questions/6294065/jaxb-namespaces-missing/6294412#6294412) – Sai Kumar Oct 05 '11 at 07:36
6

Was facing this issue, Solved by adding package-info in my package

and the following code in it:

@XmlSchema(
    namespace = "http://www.w3schools.com/xml/",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns = {
        @XmlNs(prefix="", namespaceURI="http://www.w3schools.com/xml/")
    }
)  
package com.gateway.ws.outbound.bean;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Mohan Seth
  • 761
  • 2
  • 11
  • 20
6

To specify more than one namespace to provide prefixes, use something like:

@javax.xml.bind.annotation.XmlSchema(
    namespace = "urn:oecd:ties:cbc:v1", 
    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED,
    xmlns ={@XmlNs(prefix="cbc", namespaceURI="urn:oecd:ties:cbc:v1"), 
            @XmlNs(prefix="iso", namespaceURI="urn:oecd:ties:isocbctypes:v1"),
            @XmlNs(prefix="stf", namespaceURI="urn:oecd:ties:stf:v4")})

... in package-info.java

D. Thorne
  • 61
  • 1
  • 2
  • @D.Throne can namespace's value be "urn:oecd:ties:isocbctypes:v1" or "urn:oecd:ties:stf:v4" instead of "urn:oecd:ties:cbc:v1"? if not then on what parameter we will put namespace value when there are more than one namespace prefix. – PRAFUL ANAND May 26 '19 at 19:01
  • I want to have multiple namespaces with different prefix. Could you please help me on that ? – Deepak Malav Jan 20 '23 at 13:51
3

marshaller.setProperty only works on the JAX-B marshaller from Sun. The question was regarding the JAX-B marshaller from SpringSource, which does not support setProperty.

Marko
  • 20,385
  • 13
  • 48
  • 64
A. Rick
  • 649
  • 5
  • 11
  • 2
    But the Spring Marshaller does support marshaller properties via the "marshallerProperties" map, which can be configured on your Spring marshaller definition, and it passes them down to the wrapped JAXB context implementation – Alex Mar 18 '13 at 20:21
3

Another way is to tell the marshaller to always use a certain prefix

marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {
             @Override
            public String getPreferredPrefix(String arg0, String arg1, boolean arg2) {
                return "ns1";
            }
        });'
devnoo
  • 391
  • 1
  • 6
  • 5
    this does not work in java 8 see http://stackoverflow.com/questions/2326107/what-happened-to-jaxbs-namespaceprefixmapper-in-jdk6u18 – teknopaul Jan 19 '17 at 15:46