Skip to content

Custom Namespace Prefixes

Ryan Heaton edited this page Oct 4, 2023 · 2 revisions

Custom Namespace Prefixes

Sometimes you want to customize the namespace prefixes in the generated XML documents. There are a few ways to do this.

Note: Technically speaking, it is not necessary to customize the namespace prefixes. Everything should work fine with the autogenerated namespace prefixes, and the generated XML will still be valid. The only reason to customize the namespace prefixes is to make the generated XML look prettier. If you don't care, don't bother.

Step 1: Determine the namespace to customize

You can determine the namespace by looking at the value of the "targetNamespace" attribute on the generated WSDL or schema file. It will be the namespace that is assigned by the annotations or it will be autogenerated.

For example, the namespace for the following WSDL is "http://api.mycompany.com/v1/services/":

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://api.mycompany.com/v1/services/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/">
 <types>
   ...
 </types>

 ...
</definitions>

The namespace for the following Schema is "http://api.mycompany.com/v1/data/":

<xs:schema version="1.0" targetNamespace="http://api.mycompany.com/v1/data/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 ...
</xs:schema>

The namespace for the following service endpoint interface is "http://api.mycompany.com/v1/endpoints/":

@WebService (
  targetNamespace = "http://api.mycompany.com/v1/endpoints/"
)
public interface MyService {
 ...
}

The namespace for the following JAXB root element is "http://api.mycompany.com/v1/model/":

@XmlRootElement (
  namespace = "http://api.mycompany.com/v1/model/"
)
public class MyElement {
 ...
}

The namespace for the JAXB data types in the following package (defined by package-info.java) is "http://api.mycompany.com/v1/model/":

@XmlSchema (
  namespace = "http://api.mycompany.com/v1/model/"
)
package com.webcohesion.enunciate.rt;

import jakarta.xml.bind.annotation.XmlSchema;

Step 2: Assign the prefix

You can assign the prefix either by using annotations or my using the Enunciate configuration file.

To use annotations, the prefix can be specified at the package level with a package-info.java file and the @jakarta.xml.bind.annotation.XmlSchema annotation. The following annotation at the package-level will assign the prefix "services" to the namespace "http://api.mycompany.com/v1/services" and the prefix "data" to the namespace "http://api.mycompany.com/v1/data":

@XmlSchema (
 xmlns = {
   @XmlNs(prefix = "services", namespaceURI="http://api.mycompany.com/v1/services"),
   @XmlNs(prefix = "data", namespaceURI="http://api.mycompany.com/v1/data")
 }
)

package com.mycompany.api;

You can also assign a prefix with the Enunciate configuration file:

<enunciate>
 <namespaces>
   <namespace uri="http://api.mycompany.com/v1/data" id="data"/>
   <namespace uri="http://api.mycompany.com/v1/services" id="services"/>
 </namespaces>
 ...
</enunciate>

Step 3: Use the Enunciate JAXB Context

Note: This does not apply to Enunciate 1.x

In order for the runtime to use the correct namespace prefixes, a namespace prefix mapper must be supplied. For details, see Enunciate Runtime Utilities.

Clone this wiki locally