Spring-WS:如何在不启动Web服务的情况下生成WSDL?

时间:2010-10-06 09:09:34

标签: spring soap spring-ws

我们使用Spring-WS作为实现Web服务的基础(使用框架生成的WSDL)。除了WAR文件之外,我们的构建还生成客户端JAR(供我们的Java客户端使用和我们自己的端到端功能测试),包括模式生成的DTO和Web服务方法的存根。这些是使用wsimport(JAX-WS)生成的。问题是这会产生一个多步骤的构建过程:

  1. 构建服务器WAR文件;
  2. 启动Tomcat(使WSDL可用);
  3. 生成客户端存根(在WSDL URL上指向wsimport)。
  4. 是否有某种方法可以在不启动Web服务的情况下生成WSDL?然后我们可以在一个步骤中构建所有内容。

2 个答案:

答案 0 :(得分:3)

此示例代码适用于Ant任务的基础:

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

答案 1 :(得分:1)

另一个选择可能是从应用程序上下文中获取已配置的bean:

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