【问题标题】:How to use WSDL with spring-boot?如何在 spring-boot 中使用 WSDL?
【发布时间】:2015-11-18 06:38:58
【问题描述】:

我有客户端提供的 WSDL 和模式文件。我需要使用这个 WSDL 文件创建 Spring-boot SOAP Web 服务。我有谷歌它和我能找到的所有例子,他们已经用 spring 自动生成 wsdl。我怎样才能使用我的 WSDL 来生成 SOAP 服务?

【问题讨论】:

  • 我建议你检查这个:spring.io/guides/gs/producing-web-service 它解释了如何创建一个契约优先的 SOAP 服务。
  • @hrrgttnchml 这里他们使用了Schema文件并通过代码生成了wsdl。我的要求是我有 WSDL 并且需要为该 WSDL 编写服务而不生成新服务。
  • 我通常不走那条路,但我建议你看看 CXF 框架的 wsdl2java。
  • 使用 xjc 工具手动编译 WSDL 并继续使用它。
  • @user3496599 你想好怎么做了吗?我也有同样的问题

标签: soap spring-boot spring-ws


【解决方案1】:

以下是将现有 wsdl 与 Spring-Ws 和 Spring-boot 一起使用的常见步骤。

配置类

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }

    //http://localhost:8080/ws/services.wsdl --bean name is set to 'services'
    @Bean(name = "services")
    public Wsdl11Definition defaultWsdl11Definition() {
        SimpleWsdl11Definition wsdl11Definition = new SimpleWsdl11Definition();
        wsdl11Definition.setWsdl(new ClassPathResource("/schema/MyWsdl.wsdl")); //your wsdl location
        return wsdl11Definition;
    }
}
  1. 在您的 pom.xml 中使用“jaxb2-maven-plugin”插件从您的 wsdl 生成类。
  2. 在 Spring-WS 中,您必须自己编写端点。没有为端点生成代码。它很容易写。您可以按照 spring 网站上的教程/指南进行操作。

【讨论】:

【解决方案2】:

有许多选项可用于从 WSDL 文件开始并使用 Spring Boot 公开 Web 服务。您通常会根据 WSDL 定义生成 Java 类。有许多JAXB Maven plugins 会支持你这样做。

此外,在使用 Spring Boot 时,请确保利用 spring-boot-starters 来自动管理所需的不同依赖项。

一种方法是将 Spring Web Services 与 maven-jaxb2-plugin 插件结合使用。 我创建了一个step by step tutorial which explains how to do this using Spring-WS starting from a WSDL file for both consumer and provider

另一种选择是将 Apache CXF 之类的框架与 cxf-codegen-plugin 插件结合使用。 CXF 还带有它自己的CXF Spring Boot starter,称为cxf-spring-boot-starter-jaxws。为了帮助您入门,我编写了一个example which uses the CXF starter in combination with Spring Boot to create a web service starting from a WSDL file

【讨论】:

  • 感谢分享分步教程。
  • 用户@codeNotFound 写的教程对我的帮助超过了当前的置顶评论
【解决方案3】:

最简单的方法是简单地使用cxf-spring-boot-starter incl。这是companion Maven plugin,他们将负责从您的wsdl 和架构文件 中生成所需的大部分内容。这是一个完整的例子:https://github.com/codecentric/spring-samples/tree/master/cxf-boot-simple

使用 pom.xml 中的启动器,您只需将 wsdl 和模式文件放在 src/main/resources 中即可。这是一个完整的例子pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.codecentric.soap</groupId>
    <artifactId>cxf-boot-simple</artifactId>
    <version>2.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cxf-boot-simple</name>
    <description>Demo project for using Spring Boot Starter CXF</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>cxf-spring-boot-starter</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>de.codecentric</groupId>
                <artifactId>cxf-spring-boot-starter-maven-plugin</artifactId>
                <version>2.0.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

【讨论】:

    【解决方案4】:

    您可以在您的包中创建 WebServiceConfiguration java 类。

    @EnableWs
    @Configuration
    public class WebServiceConfig extends WsConfigurerAdapter {
    
        @Bean
        public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
            MessageDispatcherServlet servlet = new MessageDispatcherServlet();
            servlet.setApplicationContext(applicationContext);
            servlet.setTransformWsdlLocations(true);
            return new ServletRegistrationBean(servlet, "/ProjectName/*");
        }
        @Bean(name = "wsdlname")
        public DefaultWsdl11Definition defaultWsdl11Definition (XsdSchema cityRequestSchema) {
            DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
            wsdl11Definition.setRequestSuffix("ByCountry");
            wsdl11Definition.setResponseSuffix("City");
            wsdl11Definition.setPortTypeName("Hotelport");
            wsdl11Definition.setLocationUri("/ProjectName");
            wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
            wsdl11Definition.setSchema(cityRequestSchema);
            return wsdl11Definition;
    
        }
    
        @Bean
    public XsdSchema cityRequestSchema() {
        return new SimpleXsdSchema(new ClassPathResource("CityRequest.xsd"));
    }
    

    作为 Spring Boot 应用程序运行后...然后将此 URL 复制粘贴到您的浏览器中。 http://localhost:8080/ProjectName/wsdlname.wsdl

    注意:localhost:8080 替换为您的 tomcat 端口

    【讨论】:

      【解决方案5】:
      • 首先为请求和响应定义定义 XSD。

      • 然后配置端点。 (即创建一个 Bean 类和一个控制器类)

      • 然后配置 Message Dispatcher Servlet 以接收请求。

      • 然后在 pom.xml 中添加 wsdl4j 依赖。

      • 然后添加如下 Web 服务配置类。

        @Bean(name = "students")
          public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema studentsSchema) {
            DefaultWsdl11Definition definition = new DefaultWsdl11Definition();
            definition.setPortTypeName("StudentPort");
            definition.setTargetNamespace("http://in28minutes.com/students");
            definition.setLocationUri("/ws");
            definition.setSchema(studentsSchema);
            return definition;
          }
        
          @Bean
          public XsdSchema studentsSchema() {
            return new SimpleXsdSchema(new ClassPathResource("student-details.xsd"));
          }
        

      然后,如果您启动 spring boot 并访问 wsdl url,那么您就可以看到 wsdl 链接。有关详细信息,请参阅此博客[1]。

      [1]https://dzone.com/articles/creating-a-soap-web-service-with-spring-boot-start

      【讨论】:

        猜你喜欢
        • 2019-02-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-19
        • 2020-11-15
        • 2015-08-26
        相关资源
        最近更新 更多