1. 程式人生 > >SpringBoot整合cxf釋出WebService服務和客戶端呼叫WebService服務

SpringBoot整合cxf釋出WebService服務和客戶端呼叫WebService服務

最近在做公司專案的一個功能需要寫WebSerice介面,為了系統得學習WebService,決定寫一個測試介面的例子。
測試專案中使用的是SpringBoot(spring整合cxf需新增cxf-rt-frontend-jaxws,cxf-rt-transports-http依賴)

新增依賴

<?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>com.wyj</groupId>
    <artifactId>wyj-interface-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>wyj-interface-service</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <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>

        <!-- http -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.4</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>
        <!-- 熱部署模組 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 這個需要為 true 熱部署才有效 -->
        </dependency>

        <!-- CXF webservice -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
            <version>3.1.11</version>
        </dependency>
        <!-- CXF webservice -->

        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.11.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

服務端介面

package com.wyj.webservice;

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

/**
 * webservice測試介面
 * 
 * 
 * @author:WangYuanJun
 * @date:2017年12月19日 下午9:36:49
 */
@WebService(name = "TestService", // 暴露服務名稱
targetNamespace = "http://service.wyj.com"// 名稱空間,一般是介面的包名倒序
)
public interface TestService {

    @WebMethod
    @WebResult(name = "String", targetNamespace = "")
    String sendMessage(@WebParam(name = "username") String username);
}

服務端介面實現

package com.wyj.webservice;

import javax.jws.WebService;

import org.springframework.stereotype.Component;

    /**
     * webservice測試介面實現
     * 
     * 
     * @author:WangYuanJun
     * @date:2017年12月19日 下午9:37:20
     */
    @WebService(serviceName = "TestService", // 與介面中指定的name一致
    targetNamespace = "http://service.wyj.com", // 與介面中的名稱空間一致,一般是介面的包名倒
    endpointInterface = "com.wyj.webservice.TestService"// 介面地址
    )
    @Component
    public class TestServiceImpl implements TestService {

        @Override
        public String sendMessage(String username) {

            return "hello "+username;
        }

    }

cxf配置

package com.wyj.webservice;

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
 * cxf配置
 * 
 * 
 * @author:WangYuanJun
 * @date:2017年12月19日 下午9:38:24
 */
@Configuration
public class CxfConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private TestService testService;

    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(bus, testService);
        endpoint.publish("/TestService");
        return endpoint;
    }
}

預設服務在Host:port/services/*路徑下
將TestService介面釋出在了路徑/services/TestService下,wsdl文件路徑為,http://localhost:8080/services/TestService?wsdl

TestService的wsdl資訊

<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://service.wyj.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="TestService" targetNamespace="http://service.wyj.com">
  <wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.wyj.com" elementFormDefault="unqualified" targetNamespace="http://service.wyj.com" version="1.0">

  <xs:element name="sendMessage" type="tns:sendMessage"/>

  <xs:element name="sendMessageResponse" type="tns:sendMessageResponse"/>

  <xs:complexType name="sendMessage">
    <xs:sequence>
      <xs:element minOccurs="0" name="username" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="sendMessageResponse">
    <xs:sequence>
      <xs:element minOccurs="0" name="String" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>

</xs:schema>
  </wsdl:types>
  <wsdl:message name="sendMessage">
    <wsdl:part element="tns:sendMessage" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:message name="sendMessageResponse">
    <wsdl:part element="tns:sendMessageResponse" name="parameters">
    </wsdl:part>
  </wsdl:message>
  <wsdl:portType name="TestService">
    <wsdl:operation name="sendMessage">
      <wsdl:input message="tns:sendMessage" name="sendMessage">
    </wsdl:input>
      <wsdl:output message="tns:sendMessageResponse" name="sendMessageResponse">
    </wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="TestServiceSoapBinding" type="tns:TestService">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="sendMessage">
      <soap:operation soapAction="" style="document"/>
      <wsdl:input name="sendMessage">
        <soap:body use="literal"/>
      </wsdl:input>
      <wsdl:output name="sendMessageResponse">
        <soap:body use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="TestService">
    <wsdl:port binding="tns:TestServiceSoapBinding" name="TestServiceImplPort">
      <soap:address location="http://localhost:8080/services/TestService"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

基於cxf的客戶端呼叫webservice介面

package webservice;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.junit.Test;

/**
 * webservice客戶端
 * 
 * 
 * @author:WangYuanJun
 * @date:2017年12月19日 下午9:39:49
 */
public class WebServiceTest {

    @Test
    public void testSend1(){

        // 建立動態客戶端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/services/TestService?wsdl");

        // 需要密碼的情況需要加上使用者名稱和密碼
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,PASS_WORD));
        Object[] objects = new Object[0];
        try {

            // invoke("方法名",引數1,引數2,引數3....);
            objects = client.invoke("sendMessage", "wyj");
            System.out.println("返回資料:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}

呼叫後返回結果輸出為

這裡寫圖片描述

專案地址