SpringBoot 整合 webservice 示例


現在有一些老項目是使用webservice提供遠程接口服務的,工作中遇到了,在這里使用Apache CXF 特意做一個示例,供參考!

1.webservice技術特點?

  • webservice接口調用可以跨語言(不管程序是用什么語言開發的,webservice接口都可以相互調用)
  • 也是基於http協議
  • 可以使用xml進行數據傳遞(使用jaxws協議),也可以使用json進行數據傳遞(使用jaxrs協議),本示例演示xml進行數據傳遞

2.構建webservice服務端

1、新建一個SringBoot工程,引入依賴,注意:這里不用引入web依賴,springboot 整合 webservice 依賴中包含web依賴

<!--springboot 整合 webservice 依賴-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<!--cxf 核心依賴-->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-transports-http</artifactId>
    <version>3.4.3</version>
</dependency>
<!--jaxws 協議的 cxf webservice依賴 -->
<dependency>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-rt-frontend-jaxws</artifactId>
    <version>3.4.4</version>
</dependency>

2、新建一個接口服務

@WebService(targetNamespace = "http://service.winson.com", name = "HelloService")//與接口中的命名空間一致,一般是接口的包名倒(要與實現類定義的值相同,否則利用動態代理創建客戶端,無法調用接口方法)
public interface HelloService {
    @WebMethod
    String hello(@WebParam(name = "name") String name);
}

3、接口實現類。注:目標命名空間必須與接口一致

@Service
@WebService(serviceName = "HelloService",// 與接口中指定的name一致
        targetNamespace = "http://service.winson.com",// 與接口中的命名空間一致,一般是接口的包名倒
        endpointInterface = "com.winson.service.HelloService")// 接口地址
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return name + ",welcome to china!";
    }
}

4、新建一個配置類,注冊服務,暴露服務調用地址

@Configuration
public class WebServiceConfig {

    @Autowired
    private HelloService helloService;

    /**
     * 注入servlet  bean name不能dispatcherServlet 否則會覆蓋dispatcherServlet
     *
     * @return
     */
    @Bean
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");//第二個參數:設置CXFServlet注冊地址
    }

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

    /**
     * 注冊WebService接口到webservice服務
     *
     * @return
     */
    @Bean(name = "helloServiceEndpoint")
    public Endpoint sweptPayEndpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), helloService);
        endpoint.publish("/webservice");//設置接口注冊地址
        return endpoint;
    }
}

5、訪問接口服務

WSDL:webservice 描述語言,如何解讀不贅述,查看規則是從下到上分析來看

地址:http://localhost:8080/ws/webservice?wsdl

地址格式: (http://ip+端口/cxfServlet注冊地址/具體服務接口地址+ ?wsdl)

報文示例:

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.winson.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloService" targetNamespace="http://service.winson.com">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.winson.com" elementFormDefault="unqualified" targetNamespace="http://service.winson.com" version="1.0">
<xs:element name="hello" type="tns:hello"/>
<xs:element name="helloResponse" type="tns:helloResponse"/>
<xs:complexType name="hello">
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="helloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="hello">
<wsdl:part element="tns:hello" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:message name="helloResponse">
<wsdl:part element="tns:helloResponse" name="parameters"> </wsdl:part>
</wsdl:message>
<wsdl:portType name="HelloService">
<wsdl:operation name="hello">
<wsdl:input message="tns:hello" name="hello"> </wsdl:input>
<wsdl:output message="tns:helloResponse" name="helloResponse"> </wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloServiceSoapBinding" type="tns:HelloService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="hello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="hello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="helloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloService">
<wsdl:port binding="tns:HelloServiceSoapBinding" name="HelloServiceImplPort">
<soap:address location="http://localhost:8080/ws/webservice"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

3.構建webservice客戶端

1、構建一個 SpringBoot 工程,引入依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
    <!--webservice 依賴-->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.4.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>3.4.4</version>
    </dependency>
    <!--測試依賴-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
</dependencies>

有兩種方式測試:

  • 方式1:代理類工廠的方式,需要拿到對方的接口

    @Test
    public void testWebserviceServer() {
        try {
            // 接口地址
            String address = "http://localhost:8080/ws/webservice?wsdl";
            // 代理工廠
            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
            // 設置代理地址
            jaxWsProxyFactoryBean.setAddress(address);
            // 設置接口類型
            jaxWsProxyFactoryBean.setServiceClass(HelloService.class);
            // 創建一個代理接口實現
            HelloService helloService = (HelloService) jaxWsProxyFactoryBean.create();
            // 調用代理接口的方法調用並返回結果
            String res = helloService.hello("winson");
            System.out.println("返回結果:" + res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
  • 方式2:動態調用方式,優點:動態客戶端調用,利用反射不需要引入服務端Service類

    @Test
    public void test02() {
        //創建動態客戶端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        try {
            Client client = dcf.createClient("http://localhost:8080/ws/webservice?wsdl");
            // 需要密碼的情況需要加上用戶名和密碼
            // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
            Object[] objects = new Object[0];
            // invoke("方法名",參數1,參數2,參數3....);
            objects = client.invoke("hello", "elnimo");
            System.out.println("返回數據:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
    

4.啟動測試用例,返回結果

返回數據:elnimo,welcome to china!
Disconnected from the target VM, address: '127.0.0.1:51792', transport: 'socket'

Process finished with exit code 0

以上幾步就是利用SpringBoot + webservice(jaxws) 實現webservice示例,代碼中的注釋很重要,請仔細參考,否則啟動程序會報錯。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM