Springboot项目实现webservice接口(支持soap1.1协议和soap1.2协议)

  • Post author:
  • Post category:其他




新建项目

  1. 打开idea,新建Springboot项目 File->New->Project->选择Spring Initializr->next

    在这里插入图片描述

    在这里插入图片描述



pom

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

    <groupId>com.demo</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
        <cxf.version>3.2.4</cxf.version>
        <xstream.version>1.4.8</xstream.version>
    </properties>
    
    <dependencies>
        <!--springboot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <!--<cxf>-->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>

        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>${xstream.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.json</groupId>
            <artifactId>javax.json-api</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <excludes>
                        <exclude>application.properties</exclude>
                        <exclude>default.properties</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
        <finalName>demo</finalName>
    </build>



配置

server.port=8058
spring.application.name=demo
server.tomcat.uri-encoding=UTF-8






启动类
package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication{

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}


配置类
package com.demo.ws.wsInterface;

import com.demo.ws.wsInterface.impl.WebServicesImpl1_1;
import com.demo.ws.wsInterface.impl.WebServicesImpl1_2;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class WebServicesConfiguration {
    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

   @Bean(name = "wbsBean")
    public ServletRegistrationBean dispatcherServlet() {
        ServletRegistrationBean wbsServlet = new ServletRegistrationBean(new CXFServlet(), "/demo/*");
        return wbsServlet;
    }

    /**
     * SOAP协议 1.1
     */
    @Bean
    public Endpoint endpointPurchase1_1(SpringBus springBus, WebServicesImpl1_1 service) {
        EndpointImpl endpoint = new EndpointImpl(springBus, service);
        endpoint.publish("/services");
        System.out.println("soap1.1服务发布成功!地址为:http://127.0.0.1:8058/demo/services");
        return endpoint;
    }

    /**
     * SOAP协议 1.2
     */
    @Bean
    public Endpoint endpointPurchase1_2(SpringBus springBus, WebServicesImpl1_2 service) {
        EndpointImpl endpoint = new EndpointImpl(springBus, service);
        endpoint.publish("/services");
        System.out.println("soap1.2服务发布成功!地址为:http://127.0.0.1:8058/demo/services");
        return endpoint;
    }
}



WSDL接口
package com.demo.ws.wsInterface;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface IWebServices {

    @WebMethod(action = "http://wsInterface.ws.demo.com/MethodName")
    String MethodName(@WebParam(targetNamespace = "http://wsInterface.ws.demo.com/") String inParamXml);

}


soap1.1协议实现类
package com.demo.ws.wsInterface.impl;

import com.demo.ws.wsInterface.IWebServices;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;


/* SOAP协议 1.1 */
@BindingType(value = SOAPBinding.SOAP11HTTP_BINDING)
@WebService
@Service
public class WebServicesImpl1_1 implements IWebServices{

    @Override
    public String WebMethod(String inParamXml) {
        return "";
    }
}


soap1.2协议实现类
package com.demo.ws.wsInterface.impl;

import com.demo.ws.wsInterface.IWebServices;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;


/* SOAP协议 1.2 */
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
@WebService
@Service
public class WebServicesImpl1_2 implements IWebServices{

    @Override
    public String WebMethod(String inParamXml) {
        return "";
    }
}



版权声明:本文为mianxiao15908原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。