1. 程式人生 > >Spring Boot整合CXF專案

Spring Boot整合CXF專案

                                                                                                                     2018年12月29日,元旦放假,浙江紹興

昨天做了一個Spring Boot整合CXF的簡單小栗子。今天放假沒事,跑媽媽這邊來玩,晚上趁著這個雪天沒事,記錄一下專案建立過程和思路。

當前專案結構如下(後面會補充幾種呼叫Web Service的方式的程式碼):

POM.XML檔案也比較簡單,就是在Spring Boot 的initalizer的時候把spring boot版本降到2.07,然後在web項裡選擇Apache CXF(JAX-RS)自動引入的依賴,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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.WebService</groupId>
    <artifactId>spring-cxf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cxf</name>
    <packaging>war</packaging>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring.boot.version>1.6.6</spring.boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
            <version>3.2.5</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.6</version>
        </dependency>




    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

此處我們全部使用註解,儘量少的使用配置。

MyTestService的程式碼如下

package com.webservice.springcxf.service;

import javax.jws.WebService;

/**
 * @Author: gtd
 * @Description:
 * @Date: Create in 22:03 2018/12/29
 */
@WebService
public interface MyTestService {

    String print(String name);
}

實現類MyTestServiceImpl的程式碼如下:

package com.webservice.springcxf.service.impl;

import com.webservice.springcxf.service.MyTestService;
import org.springframework.stereotype.Component;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.ws.WebEndpoint;

/**
 * @Author: 高鐵墩
 * @Description:
 * @Date: Create in 22:18 2018/12/29
 */
@WebService(serviceName = "myTestService", // 與介面中指定的name一致
        endpointInterface = "com.webservice.springcxf.service.MyTestService")// 介面地址
@Component
public class MyTestServiceImpl implements MyTestService {

    @WebMethod
    public String print(@WebParam (name = "username") String name) {
        return "hello," + name;
    }

}

我們的配置類WebServiceConfig程式碼如下(在載入配置類的時候,釋出我們的介面)。

package com.webservice.springcxf.config;

import com.webservice.springcxf.service.MyTestService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

/**
 * @Author: gtd
 * @Description:
 * @Date: Create in 20:20 2018/12/30
 */
@Configuration
public class WebServiceConfig {
    @Autowired
    private Bus bus;

    @Autowired
    @Qualifier("myTestServiceImpl")
    private MyTestService myTestService;

    @Bean
    public Endpoint endpoint(){
        Endpoint endpoint = new EndpointImpl(bus,myTestService);    //建立Endpoint例項
        endpoint.publish("/myService");                     //設定釋出地址併發布
        return endpoint;
    }

}

當然,我們也可以使用通過配置的方式釋出,這裡我把我第一次通過配置釋出方式的配置檔案程式碼也列出來吧,供大家參考:

applicationContext.xml 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://cxf.apache.org/jaxws
       http://cxf.apache.org/schemas/jaxws.xsd
        ">

    <context:component-scan base-package="com.webservice.springcxf"/>

    <jaxws:endpoint id="myTestWebService"
        implementor="com.webservice.springcxf.service.impl.MyTestServiceImpl"
        address="/myService"
    />


</beans>

 

 

兩種方式任選其一,執行啟動類,在瀏覽器輸入:http://127.0.0.1:8082/WebService/services/myService?wsdl

返回截圖:

今天先到此位置,後面繼續補充客戶端程式碼和其他呼叫方式程式碼,以及對CXF的原始碼在其他文章裡分析。晚安~~

原始碼地址:[email protected]:tiedungao/spring-cxf.git