Codehaus XFire

Documentation

Quicklinks

Developers

Sponsors

Creating a service

Steps

  1. Create your Schema
  2. Create your XMLBeans
  3. Create your Service
  4. Register your service

Assuming that you know how to do steps 1 and 2, lets pick up at step 3. Create your service class:

package org.codehaus.xfire.xmlbeans;

import net.webservicex.GetWeatherByZipCodeDocument;
import net.webservicex.GetWeatherByZipCodeResponseDocument;
import net.webservicex.WeatherForecasts;

public class WeatherService
{
    public GetWeatherByZipCodeResponseDocument GetWeatherByZipCode( GetWeatherByZipCodeDocument body )
    {
        GetWeatherByZipCodeResponseDocument res =
            GetWeatherByZipCodeResponseDocument.Factory.newInstance();

        WeatherForecasts weather =
            res.addNewGetWeatherByZipCodeResponse().addNewGetWeatherByZipCodeResult();

        weather.setLatitude(1);
        weather.setLongitude(1);
        weather.setPlaceName("Grand Rapids, MI");

        return res;
    }
}

Notice that we used the "Document" Types, not the "GetWeatherByZipCode" class. After you've created your service class you simply need to register it:

XFire xfire = XFireFactory.newInstance().getXFire();
XmlBeansServiceFactory factory = new XmlBeansServiceFactory(xfire.getTransportManager());

Service service = factory.create(WeatherService.class);

xfire.getServiceRegistry().register(service);

XFire will now expose your service as the "WeatherService" and will generate WSDL for you automatically!

XMLBeans with JSR 181

You may use your XMLBeans service with JSR 181 annotations. You need to set up a special service factory to create your service though:

import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.xmlbeans.XmlBeansWSDLBuilderFactory;

AnnotationServiceFactory asf = new AnnotationServiceFactory();
// Set up the XMLBeans binding
AegisBindingProvider provider = new AegisBindingProvider(new XmlBeansTypeRegistry());
asf.setBindingProvider(provider);

// Tell XFire to use the schemas from XMLBeans in the WSDL
asf.setWsdlBuilderFactory(new XmlBeansWSDLBuilderFactory());

Then you can create your service:

Service service = asf.create(MyService.class);

Problems with XMLBeans (XMLBeans Namespace Hack)

Due to some bugs in how XMLBeans handles StAX streams you may need to enable the "xmlbeans namespace hack". This just tells xfire to use the XMLBeans DOM to write to the outgoing xml stream instead of using the XMLBeans XMLStreamReader. You will need to enable this when:
1. You are using RPC/Literal services
2. Any of your operations contain parameters that are <complexType>s instead of <element>s in your schema.

To enable the fix simply do:

import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.xmlbeans.XmlBeansType;

Service service = ...;
service.setProperty(XmlBeansType.XMLBEANS_NAMESPACE_HACK, "true");