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

发布于 2024-09-26 11:03:08 字数 364 浏览 1 评论 0原文

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

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

是否有某种方法无需启动 Web 服务即可生成 WSDL? 然后我们可以一步构建所有内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-10-03 11:03:08

此示例代码适用于 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();
}

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();
}
最丧也最甜 2024-10-03 11:03:08

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

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());
            }
        }        
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文