views:

25

answers:

1

We use Spring-WS as the basis for implementing a web service (with the WSDL generated by the framework). As well as a WAR file our build produces a client side JAR (for use by our Java clients and our own end-to-end functional tests) consisting of schema generated DTOs and stubs for the web service methods. These are generated using wsimport (JAX-WS). Problem is this gives rise to a multi-step build process:

  1. Build the server WAR file;
  2. Start Tomcat (to make the WSDL available);
  3. Generate the client side stubs (pointing wsimport at the WSDL url).

Is there some way to generate the WSDL without having to start the web service? Then we could build everything in a single step.

+2  A: 

This example code is suitable for the basis of an Ant task:

import javax.xml.stream.XMLStreamException;
import javax.xml.transform.TransformerFactory;

import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
import org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection;

....

private String generateWsdlFromSpringInfrastructure() throws Exception
{
    // We only need to specify the top-level XSD
    FileSystemResource messagesXsdResource = new FileSystemResource("C:/MyProject/xsds/my-messages.xsd");
    CommonsXsdSchemaCollection schemaCollection = new CommonsXsdSchemaCollection(new Resource[] {messagesXsdResource});
    // In-line all the included schemas into the including schema
    schemaCollection.setInline(true);
    schemaCollection.afterPropertiesSet();

    DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
    definition.setSchemaCollection(schemaCollection);
    definition.setPortTypeName(portTypeName);
    definition.setLocationUri("http://localhost:8080/myProject/services");
    definition.setTargetNamespace("http://www.acme.com/my-project/definitions");
    definition.afterPropertiesSet();

    StringResult wsdlResult = new StringResult();
    TransformerFactory.newInstance().newTransformer().transform(definition.getSource(), wsdlResult);
    return wsdlResult.toString();
}
David Easley