如何在spring-boot中使用WSDL?

时间:2015-11-18 06:38:59

标签: soap spring-boot spring-ws

我有客户端提供的WSDL和模式文件。我需要使用此WSDL文件创建Spring-boot SOAP Web服务。我有谷歌和我能找到的所有例子,他们用spring自动生成wsdl。我如何使用我的WSDL生成SOAP服务?

5 个答案:

答案 0 :(得分:16)

以下是在Spring-Ws和Spring-boot中使用现有wsdl时要遵循的常用步骤。

配置类

@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中,您必须自己编写端点。没有为端点生成代码。它易于编写。你可以按照春季网站上的教程/指南进行操作。

答案 1 :(得分:7)

有许多选项可以从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的{​​{1}}。为了让您入门,我已经编译了example which uses the CXF starter in combination with Spring Boot to create a web service starting from a WSDL file

答案 2 :(得分:2)

您可以在包中创建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"));
}

作为春季启动应用运行后...然后复制粘贴此浏览器中的网址。 http://localhost:8080/ProjectName/wsdlname.wsdl

注意:localhost:8080用你的tomcat端口替换

答案 3 :(得分:0)

最简单的方法是简单地使用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 :(得分:0)

  • 首先为请求和响应定义定义XSD。

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

  • 然后配置消息分派器Servlet来接收请求。

  • 然后将wsdl4j依赖项添加到pom.xml。

  • 然后添加如下所示的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