Wednesday, March 14, 2012

how to add namespace Prefix to all the elements of JAX-WS webservice output

how to add namespace Prefix to all the elements of JAX-WS webservice output

This post will explain, how to add namespace Prefix to all the elements of JAX-WS webservice output

Sometimes we may required to have the namespace prefix (ex.ns1) defined for all the elments of the JAX-WS webservice output.

The default annotation of the package-info.java class looks like below

@javax.xml.bind.annotation.XmlSchema(namespace = "PPDS:OPIRequest", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.reuters.eai.types;

This case only the parent element will be have the namespace prefix but not the child elements.

eg.

<ns1:Employee ns1="PPDS:OPIRequest">
<name></name>
<number></number>
</ns1:Employee>

To have the namespace prefix in all the output elements,modify the above annotaion in the package-info.java class as mentioned below.

@XmlSchema( namespace = "PPDS:OPIRequest", elementFormDefault = XmlNsForm.QUALIFIED, xmlns={@XmlNs(prefix="ns3", namespaceURI="PPDS:OPIRequest")})
package com.reuters.eai.types;
import javax.xml.bind.annotation.*;

e.g

<ns1:Employee ns1="PPDS:OPIRequest">
<ns1:name></ns1:name>
<ns1:number></ns1:number>
</ns1:Employee>


5 comments:

  1. How do you change the prefix of a different package, aka namespace?

    ReplyDelete
  2. Create a package-info.java file in the package and specifiy the package name and the namespace accordingly.

    @javax.xml.bind.annotation.XmlSchema(namespace = "PPDS:OPIRequest", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package com.reuters.eai.types;

    Regards
    Albin I

    ReplyDelete
  3. Did that, this is the package the web service is called from:

    @XmlSchema (
    namespace = "",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns = {
    @XmlNs(prefix= "ns3", namespaceURI="http://plan.midtier.process.vanguard.com"),
    })
    package com.vanguard.process.midtier.plan;
    import javax.xml.bind.annotation.*;

    THis is a 2nd package the web service uses an object in:

    @XmlSchema(
    namespace = "",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns = {
    @XmlNs(prefix= "p251", namespaceURI="http://com.vanguard.process.midtier.utility/"),
    })
    package com.vanguard.process.midtier.utility;
    import javax.xml.bind.annotation.*;

    However, the soap envelope on gets the prefix from the first?







    18.2402002881
    .
    .
    .
    67.21000000

    3
    7
    2014


    The dayOfMonth, month, year are from an object in that 2nd package and they don't get the namespace?

    ReplyDelete
  4. The soap xml did not post to the comment, thinks it html I guess

    ReplyDelete
  5. ns3:yearlyReturnsAsOfDate
    dayOfMonth>30/dayOfMonth
    month>6/month
    year>2014/year

    ReplyDelete