基於Maven的Spring整合CXF

楓葉梨花發表於2018-10-15

Spring整合CXF,即通過Spring來發布WebService服務。

配置環境

JAVA_HOME

CXF下載可以訪問https://www.apache.org/dyn/closer.lua/cxf/3.2.6/apache-cxf-3.2.6.zip

CXF_HOME = cxf的目錄

Path =%JAVA_HOME%\lib;%CXF_HOME%\bin;
CLASSPATH=.;%CXF_HOME%\lib\cxf-manifest.jar
複製程式碼

建立Maven專案並匯入Jar包

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>servlet-api</artifactId>
	<version>2.5</version>
	<scope>provided</scope>
</dependency>

<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.17</version>
</dependency>

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>1.8.0-beta0</version>
</dependency>

<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>1.8.0-beta0</version>
</dependency>

<!-- Spring的Jar包 -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-beans</artifactId>
	<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-web</artifactId>
	<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context-support</artifactId>
	<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-expression</artifactId>
	<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>4.3.12.RELEASE</version>
</dependency>

<!-- CXF所需Jar包 -->
 <dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-frontend-jaxws</artifactId>
	<version>3.2.6</version>
</dependency>
<dependency>
	<groupId>org.apache.cxf</groupId>
	<artifactId>cxf-rt-transports-http</artifactId>
	<version>3.2.6</version>
</dependency>
複製程式碼

建立服務介面和實現服務類

介面類

package com.cxf.server;

public interface WeatherService{
	public String queryWeather(String cityName);
}
複製程式碼

實現類

package com.cxf.server;

import javax.jws.WebService;

@WebService
public class WeatherServiceImpl implements WeatherService{

	@Override
	public String queryWeather(String cityName) {
		
		System.out.println("from client..."+cityName);
		String result = "晴";
		
		return result;
	}
}
複製程式碼

Spring配置Bean

<?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:jaxrs="http://cxf.apache.org/jaxrs"
    xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   	http://cxf.apache.org/jaxrs
   	http://cxf.apache.org/schemas/jaxrs.xsd
   	http://cxf.apache.org/jaxws
   	http://cxf.apache.org/schemas/jaxws.xsd
   	http://cxf.apache.org/core
   	http://cxf.apache.org/schemas/core.xsd">
    
    <!-- 釋出webservice服務 -->
    <jaxws:server id="weather" address="/weather"
     serviceClass="com.cxf.server.WeatherService">
    	<jaxws:serviceBean>
    		<bean class="com.cxf.server.WeatherServiceImpl"></bean>
    	</jaxws:serviceBean>
    </jaxws:server>
    
  </beans>
複製程式碼

配置web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>cxf</servlet-name>
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>cxf</servlet-name>
		<url-pattern>/ws/*</url-pattern>
	</servlet-mapping>
	
  <display-name>webservice</display-name>
</web-app>
複製程式碼

啟動Maven專案訪問

獲取所有的服務

http://localhost:8080/webservice/ws/services
複製程式碼

服務說明

http://localhost:8080/webservice/ws/weather?wsdl
複製程式碼

客戶端編寫

客戶端的程式碼還是如上幾篇部落格同樣的方式生成呼叫。
複製程式碼

相關文章