Enterprise Java

Leveraging MOXy in your Web Service via JAX-WS Provider

In previous articles I demonstrated how EclipseLink JAXB (MOXy) is directly integrated into the JAX-WS implementations in WebLogic (as of 12.1.1) and in GlassFish (as of 3.1.2). In this post I’ll demonstrate how to leverage MOXy in any application server by using the JAX-WS Provider class.

Web Service

The Provider mechanism in JAX-WS provides you a way to create a Web Service with direct access to the XML. Through the @ServiceMode annotation you can specify whether you want all of the XML from the message or just the payload.


All the magic happens in the invoke method. Since we specified PAYLOAD as the service mode the input will be an instance of Source that represents the body of the message. All JAXB (JSR-222) implementations can unmarshal from a Source so we will do that to realize the request. After we perform our business logic we need to return the body of the response as an instance of Source. To achieve this we will wrap our response objects in an instance of JAXBSource.

package blog.jaxws.provider;

import javax.xml.bind.*;
import javax.xml.bind.util.JAXBSource;
import javax.xml.transform.Source;
import javax.xml.ws.*;

    portName = 'FindCustomerPort', 
    serviceName = 'FindCustomerService', 
    targetNamespace = 'http://service.jaxws.blog/', 
    wsdlLocation = 'WEB-INF/wsdl/FindCustomerService.wsdl')
public class FindCustomerService implements Provider<Source> {

    private JAXBContext jaxbContext;

    public FindCustomerService() {
        try {
            jaxbContext = JAXBContext.newInstance(FindCustomerResponse.class,
        } catch (JAXBException e) {
            throw new WebServiceException(e);

    public Source invoke(Source request) throws WebServiceException {
        try {
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            FindCustomerRequest fcRequest = (FindCustomerRequest) unmarshaller

            Customer customer = new Customer();

            FindCustomerResponse response = new FindCustomerResponse();

            return new JAXBSource(jaxbContext, response);
        } catch (JAXBException e) {
            throw new WebServiceException(e);


MOXy as the JAXB Provider

To specify that MOXy should be used as the JAXB provider we need to include a file called jaxb.properties that is located in the same package as our domain model with the following entry (see: Specifying EclipseLink MOXy as your JAXB Provider).



Below is the WSDL that corresponds to our Web Service. One draw back to using the Provider approach is that the JAX-WS implementation can’t automatically generate one for us (see: GlassFish 3.1.2 is full of MOXy (EclipseLink JAXB)). A WSDL is necessary as it defines a contract for the client. It can even be used to generate a client.

<?xml version='1.0' encoding='UTF-8'?>
    <message name='findCustomer'>
        <part name='parameters' element='tns:findCustomer'/>
    <message name='findCustomerResponse'>
        <part name='parameters' element='tns:findCustomerResponse'/>
    <portType name='FindCustomer'>
        <operation name='findCustomer'>
    <binding name='FindCustomerPortBinding' type='tns:FindCustomer'>
            <operation name='findCustomer'>
                <soap:operation soapAction=''/>
                    <soap:body use='literal'/>
                    <soap:body use='literal'/>
    <service name='FindCustomerService'>
        <port name='FindCustomerPort' binding='tns:FindCustomerPortBinding'>
            <soap:address location='http://localhost:8080/Blog-JAXWS/FindCustomerService'/>

XML Schema

Below is the XML schema that corresponds to the payload of our message. One draw back to using the Provider approach is that the JAX-WS implementation can’t leverage JAXB directly to automatically generate the XML schema directly, so we need to supply one.

<?xml version='1.0' encoding='UTF-8'?>
    <xsd:element name='findCustomerResponse' 
        type='ns0:findCustomerResponse' />
    <xsd:complexType name='findCustomerResponse'>
            <xsd:element name='return' type='ns0:customer'
                minOccurs='0' />
    <xsd:element name='findCustomer' 
        type='ns0:findCustomer' />
    <xsd:complexType name='findCustomer'>
            <xsd:element name='arg0' type='xsd:int' />
    <xsd:complexType name='customer'>
            <xsd:element name='personal-info' minOccurs='0'>
                        <xsd:element name='first-name' 
                            minOccurs='0' />
                        <xsd:element name='last-name'
                            minOccurs='0' />
        <xsd:attribute name='id' type='xsd:int' use='required' />

Request Objects

The highlighted portion of the XML message below is what we are going to receive in our Provider as in instance of Source. We will create a JAXB model to map to this section.

<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>
        <ns2:findCustomer xmlns:ns2='http://service.jaxws.blog/'>


The root element is in a different XML namespace than the rest of the body. We will leverage the @XmlRootElement annotation to specify the namespace (see: JAXB & Namespaces ).

package blog.jaxws.provider;

import javax.xml.bind.annotation.*;

public class FindCustomerRequest {

    private int arg0;

    public int getArg0() {
        return arg0;

    public void setArg0(int arg0) {
        this.arg0 = arg0;


Response Objects

The highlighted portion of the XML message below is what we are going to return from our Provider as in instance of Source. We will create a JAXB model to map to this section.

<S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'>
    <S:Header />
            <return id='123'>


package blog.jaxws.provider;

import javax.xml.bind.annotation.*;

public class FindCustomerResponse {

    private Customer value;

    public Customer getValue() {
        return value;

    public void setValue(Customer value) {
        this.value = value;



One of the many reasons to use MOXy is its path based mapping (see: XPath Based Mapping). Below is an example of how it is specified using the @XmlPath annotation.

package blog.jaxws.provider;

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlType(propOrder = { 'firstName', 'lastName' })
public class Customer {

    private int id;
    private String firstName;
    private String lastName;

    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

    public String getFirstName() {
        return firstName;

    public void setFirstName(String firstName) {
        this.firstName = firstName;

    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;



Reference: Leveraging MOXy in your Web Service via JAX-WS Provider from our JCG partner Blaise Doughan at the Java XML & JSON Binding blog.

Blaise Doughan

Team lead for the TopLink/EclipseLink JAXB & SDO implementations, and the Oracle representative on those specifications.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
10 years ago

Dear Blaise, Thanks for all your posts, which are very helpful. Currently I am working on a mobility project consuming the webservices. I am using the approach of generating the stubs by using wsimport and JAX Context generating classes by using XJC. I am able to Invoke Webservice Operation With the simple type, There are two more Webservices with the ComplexTypes where I need to Pass Parameters(Attribute and ComplexType Object). Bu=y using AnnotationIntrospector I am able to see the Newly Created Object Instance(with all the Setted Values) but while passing this Object instance to the WebService opration, It looks like… Read more »

Back to top button