How to Use SOAP Web Services with Java and Flex: Part 1 of 2

We recently took on a project at Black Pepper which required a Flex front end that talked to a server with all of the business logic and data.

Our set of technologies for the server side was this:

So, pretty much our usual setup that we've done a load of times, except for the XFire part which was new...and the focus of this blog entry.

In essence, XFire enables you to very easily expose Spring objects as SOAP web services. In our case we achieved this using the XFire servlet method by adding these lines to our web.xml:

xfire
org.codehaus.xfire.spring.XFireSpringServlet

xfire
/services/*

You'll also need an xfire-servlet.xml file in your WEB-INF directory:

This basically says that XFire should intercept any requests coming into /services/* and do it's magic. XFire's magic is that it will output SOAP for your services and convert the input of SOAP requests into nice Java objects and invoke methods on your services with those objects. One the Java and Spring side of things, you just need to setup a POJO to do some work. I'll use our CountryManager as an example:

@WebService
public interface CountryManager {
    @WebResult(name = "countries")
    List getCountries();

    @Transactional
    @WebResult(name="country")
    public Country create(@WebParam(name="name") String name);

    @Transactional
    @WebResult(name="country")
    public Country amend(@WebParam(name="country") Country country);

    @Transactional
    public void remove(@WebParam(name="id") Long countryId);
}

As you can see from this CountryMaanger interface, we just mark it up with the @WebService annotation and then add @WebResult and @WebParam annotations to the methods. Also note the @Transactional annotations showing that we are using Springs annotation based transaction management. The implementation of CountryManager is CountryManagerImpl:

@WebService(serviceName = "CountryService", endpointInterface = "blackpepper.service.CountryManager")
@ServiceProperties(properties={@ServiceProperty(key="wsdlBuilder.generateImports", value="true")})
public class CountryManagerImpl implements CountryManager {
    private final CountryDao dao;
    
    public CountryManagerImpl(CountryDao dao) {
        this.dao = dao;
    }

    public List getCountries() {
        return dao.getAll();
    }
}

The code that really sticks out here is the @WebService and @ServiceProperties annotations. One of the tricks we had to learn was that XFire didn't by default generate all of the right schema imports in the WSDL files that it generates for you. By adding the "@ServiceProperties.....wsdlBuilder.generateImports..." annotation, it forces the WSDL to be more complete. Some SOAP consumers mighht not care about this, but Flex does. Of course you need to wire your CountryManager up with Spring:

If you now fire this up and go to http://<yourserver>/project/services/CountryService?wsdl then you'll see the WSDL that XFire has generated for you for your CountryService. Now you're ready to test out your SOAP web service. You could just jump straight in and try and do the Flex code straight away, but it is best to test your service first, otherwise you won't know if it's your Flex or Java that's broken. We used the brilliant soapUI. With this, you can just point soapUI at your WSDL file and it will auto generate requests to all of your services that you can then fill in with data and test.

And now we're ready for some Flex in part 2...

This site uses cookies. Continue to use the site as normal if you are happy with this, or read more about cookies and how to manage them.

X