寫這篇文章主要是用於增強記憶,而我參考的是這位朋友的隨筆,鏈接如下
http://www.xiaomager.com/415.html
服務端開發過程
1.首先創建一個maven項目,如下圖

2.添加項目的依賴包以及設置相關配置
提示:首先介紹一下基礎環境 ,開發編譯器 intellij idea ,我們的jdk是1.7,tomcat是7,spring使用的是spring4,cxf准備使用3.1.4,這里特別需要說明的是,cxf 3.0以后的版本只能在jdk1.7上使用,如果在1.6使用的話,會直接報錯的。
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rainsoft</groupId> <artifactId>cxfWebservice</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>cxfWebservice</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.7</java.version> <!-- spring版本號 --> <spring.version>4.3.2.RELEASE</spring.version> <!-- CXF版本號 --> <cxf.version>3.1.4</cxf.version> </properties> <dependencies> <!-- spring核心包 --> <!-- springframe start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <!-- CXF webservice --> <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>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>${cxf.version}</version> </dependency> </dependencies> <build> <finalName>cxfWebservice</finalName> <defaultGoal>compile</defaultGoal> <resources> <resource> <directory>src/main/resources</directory> <!-- 資源根目錄排除各環境的配置,使用單獨的資源目錄來指定 --> <excludes> <!--測試環境--> <exclude>conf/test/*</exclude> <!--生產環境--> <exclude>conf/production/*</exclude> <!--開發環境--> <exclude>conf/development/*</exclude> </excludes> </resource> <resource> <directory>src/main/resources/conf/${profiles.active}</directory> <targetPath>conf</targetPath> </resource> </resources> <plugins> <!-- compiler插件, 設定JDK版本 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> <showWarnings>true</showWarnings> <encoding>${project.build.sourceEncoding}</encoding> <compilerArguments> <verbose /> <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> </compilerArguments> </configuration> </plugin> <!-- 解決maven命令console出現中文亂碼 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.7.2</version> <configuration> <forkMode>once</forkMode> <argLine>-Dfile.encoding=UTF-8</argLine> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> <distributionManagement> <repository> <id>nexus-releases</id> <url>http://192.168.1.22:8081/nexus/content/repositories/releases/</url> </repository> <snapshotRepository> <id>nexus-snapshots</id> <url>http://192.168.1.22:8081/nexus/content/repositories/releases/</url> </snapshotRepository> </distributionManagement> <profiles> <!--生產環境--> <profile> <id>production</id> <properties> <profiles.active>production</profiles.active> </properties> </profile> <!--測試環境--> <profile> <id>test</id> <properties> <profiles.active>test</profiles.active> </properties> </profile> <profile> <id>development</id> <properties> <profiles.active>development</profiles.active> </properties> <activation><activeByDefault>true</activeByDefault></activation> </profile> </profiles> </project>
保存后,需要執行maven 的 clean, install 來下載jar包到maven的本地庫。
3.添加配置文件支持
一共有三處配置文件需要更新
第一處:需要新增一個cxf的配置文件,這里取名為cxf-servlet.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:jaxws="http://cxf.apache.org/jaxws" xmlns:context="http://www.springframework.org/schema/context" 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.daisy" /> <bean id="cxfdemo" class="com.daisy.cxf.server.impl.MyCxfServerImpl"> </bean> <jaxws:endpoint id="cxfService" implementor="#cxfdemo" address="/cxfserver" /> </beans>
此處需要根據自己的包以及配置文件,添加配置
這里面主要就是定義了一個cxfService,它的 實現類是com.daisy.cxf.server.impl.CxfServerImpl,這個 實現類我們在第三步驟來加上,其次還定義了一個/cxfserver的路徑,即我們的cxfserver服務端的請求路徑。
第二處:需要在spring主配置applicationContext文件里把這個新建的文件添加上,配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config /> <import resource="cxf-servlet.xml" /> </beans>
第三處:就是需要在web.xml里面配置cxf的servlet,具體如下:
該處定義了請求webService的前綴,及通過引入一個org.apache.cxf.transport.servlet.CXFServlet的servlet來處理 所有前綴 /webService/*的請求,所以 我們的cxf的全路徑應該是 這里的servlet-mapping加上第一處的address,即 /webService/cxfserver。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>cxfWebservice</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/webService/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
4. 添加cxf服務端的接口類和接口實現類,目錄結構如下

java實現代碼為 IMyCxfServer.java
此處必須聲明一下,在接口的上面也必須加上 @WebService , 我開始抱着試試的心理將它去掉了,結果在客戶端調用的時候就怎么都找不到對應的方法了
package com.daisy.cxf.server; import javax.jws.WebService; /** * @author daisy * @description IMyCxfServer * @date 2017/4/8 */ @WebService public interface IMyCxfServer { String sayHello(String name); }
MyCxfServerImpl.java
package com.daisy.cxf.server.impl; import com.daisy.cxf.server.IMyCxfServer; import javax.jws.WebService; /** * @author daisy * @description MyCxfServerImpl * @date 2017/4/8 */ @WebService public class MyCxfServerImpl implements IMyCxfServer { @Override public String sayHello(String name) { return "hello "+name; } }
以上代碼表示的意思很明白,即服務端提供一個sayHello的方法,將客戶端傳遞的字符串參數 前面加上 hello 后返回。
5. 添加cxf服務端的接口類和接口實現類,目錄結構如下
將項目添加到tomcat服務器上運行,訪問鏈接 http://localhost:9090/daisyCxf//webService/cxfserver?wsdl 即可看到結果,如下圖所示

