Creating and running a fake web service from a WSDL

I needed to develop some Apex web service callout code – never a pleasant experience see e.g. When “Generate from WSDL” fails – hand-coding web service calls – but did not have access to a running instance of the service. Java 6 includes web service technology by default and I was pleasantly surprised how easy it is to create and run a fake web service to develop against using this…

Having run wsimport to generate the web service classes from the WSDL, creating a server implementation class is simply a matter of extending the service interface and using Eclipse’s “Add unimplemented methods” to create the server skeleton then adding a couple of annotations:

package com.claimvantage.test;

import javax.jws.WebService;
import javax.xml.ws.BindingType;

// Other imports removed

@WebService(
        serviceName = "FNCEWS40Service",
        portName = "FNCEWS40MTOMPort",
        targetNamespace = "http://www.filenet.com/ns/fnce/2006/11/ws/MTOM/wsdl",
        wsdlLocation = "com/claimvantage/test/filenet.wsdl",
        endpointInterface = "com.claimvantage.filenet.FNCEWS40PortType"
        )
@BindingType(value="http://java.sun.com/xml/ns/jaxws/2003/05/soap/bindings/HTTP/")
public class ServiceFakeImpl implements FNCEWS40PortType {

    @Override
    public ObjectSetType executeSearch(SearchRequestType request, Localization header) throws FaultResponse {
        // TODO fake implementation goes here
        return null;
    }

    // Other methods removed
}

The Endpoint class makes running this service very easy (with no separate web server or XML configuration required):

package com.claimvantage.test;

import javax.xml.ws.Endpoint;

public class ServiceRunner {

    private static final String URL = "http://localhost:9080/wsi/FNCEWS40MTOM/";

    public static void main(String[] args) {
        Endpoint endpoint = Endpoint.create(new ServiceFakeImpl());
        endpoint.publish(URL);
        System.out.println("Service running at " + URL);
    }
}

Then make the fake service accessible from the internet (and so from Force.com) by e.g. setting up firewall port forwarding.

Debug output and fake responses can be added to the fake server methods. When combined with debug logging in the Apex and a tool such as tcpmon to view the request and response headers and XML this gives full visibility to what is going on across the whole request/response cycle.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s