1

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

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

有什么方法可以生成 WSDL 而无需启动 Web 服务?然后我们可以一步构建所有内容。

4

2 回答 2

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();
}
于 2010-10-08T11:23:27.623 回答
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());
            }
        }        
    }
}
于 2016-10-06T11:42:16.970 回答