David Easley
David Easley

Reputation: 1383

Spring-WS: How generate the WSDL without having to start the web service?

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.

Upvotes: 1

Views: 3372

Answers (2)

tomasb
tomasb

Reputation: 1673

Another option may be to get already configured bean from app context:

import static org.junit.Assert.fail;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ws.wsdl.WsdlDefinition;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:yourWsdlConfig.xml"})
public class WsdlGeneratorTest {

    @Autowired
    private ApplicationContext ctx;

    /*
     * Path relative to module root (or absolute path can be used).
     */
    private static final String OUTPUT_PATH = "target";

    @Test
    public void test() {
        String names[] = ctx.getBeanNamesForType(WsdlDefinition.class);
        TransformerFactory tFactory = TransformerFactory.newInstance();
        for (String name : names) {
            String filename = OUTPUT_PATH + File.separator + name + ".wsdl";
            WsdlDefinition wd = (WsdlDefinition) ctx.getBean(name);
            Source source = wd.getSource();
            try {
                Transformer transformer = tFactory.newTransformer();
                FileOutputStream fo = new FileOutputStream(filename);
                StreamResult result = new StreamResult(fo);
                transformer.transform(source, result);
                fo.close();
            } catch (TransformerException e) {
                fail("Error generating " + filename + ": TransformerException: " + e.getMessage());
            } catch (IOException e) {
                fail("Error generating " + filename + ": IOException: " + e.getMessage());
            }
        }        
    }
}

Upvotes: 1

David Easley
David Easley

Reputation: 1383

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();
}

Upvotes: 3

Related Questions