代码之家  ›  专栏  ›  技术社区  ›  David Easley

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

  •  1
  • David Easley  · 技术社区  · 14 年前

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

    1. 构建服务器war文件;
    2. 启动tomcat(使WSDL可用);
    3. 生成客户端存根(将wsimport指向WSDL URL)。

    是否有某种方法可以在不启动Web服务的情况下生成WSDL? 然后我们可以一步一步地完成所有的工作。

    2 回复  |  直到 8 年前
        1
  •  3
  •   David Easley    14 年前

    此示例代码适用于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();
    }
    
        2
  •  1
  •   tomasb    8 年前

    另一个选项可能是从应用程序上下文获取已经配置的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());
                }
            }        
        }
    }