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