1. 程式人生 > >CXF配置的方式釋出WebService並運行於Tomcat容器

CXF配置的方式釋出WebService並運行於Tomcat容器

下面就介紹如何使用Spring配置的方式來發佈一個WebService。

新建一個Web Project ,匯入CXF所用的包。

返回資料的實體Bean

package com.service.cxf.config;

/**
 * @author jackphang
 * @date 2013-4-14
 * @description
 */
public class HiBean {
	private int id;
	private String name;
	private String address;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

}

介面類IHiServiceForCXFConfig

package com.service.cxf.config;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

/**
 * @author jackphang
 * @date 2013-4-14
 * @description
 */
// 必須加此註解、否則所有的方法將不對外暴露
@WebService
// 將SOAP的協議提升為1.2版本
@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
public interface IHiServiceForCXFConfig {

	public String sayHi(String str);

	public HiBean findHiById(int id);
}


實現類HiServiceImplForCXFConfig

package com.service.cxf.config;

/**
 * @author jackphang
 * @date 2013-4-14
 * @description
 */
public class HiServiceImplForCXFConfig implements IHiServiceForCXFConfig {

	@Override
	public String sayHi(String str) {
		System.out.println("sayHi被呼叫,客戶端發來資料:" + str);
		return "hi ," + str;
	}

	/**
	 * 返回一個實體HiBean
	 */
	@Override
	public HiBean findHiById(int id) {
		HiBean bean = new HiBean();
		bean.setId(id);
		bean.setName("jack");
		bean.setAddress("湖南");
		return bean;
	}
}


接下來就是配置剛剛寫好的服務類了,src的com.service.cxf.config 包中.新建一個cxf-servlet.xml,位置可以任意,如果你沒有指定cxf讀取cxf-servlet的檔案路徑,那麼cxf預設是讀取WEB-INF下的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:soap="http://cxf.apache.org/bindings/soap"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">

	<!-- 引入CXF Bean定義如下,早起的版本使用 -->
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />


	<!-- 釋出一個簡單服務(不帶介面型別),address:請求的url, implementor:被髮布的類 ,需要加註解@WebService,否則所有的方法將不對外暴露,亦不報錯 -->
<!-- 	<jaxws:endpoint id="helloServiceCXFForConfig"
		implementor="com.service.cxf.config.HelloServiceForCXFConfig" address="/hello">
		請求訊息攔截器
		<jaxws:inInterceptors>
			<bean id="inInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:inInterceptors>
		相應訊息攔截器
		<jaxws:outInterceptors>
			<bean id="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:outInterceptors>
	</jaxws:endpoint> -->

	<!-- 第二種方式釋出(帶介面型別) -->
	<!-- serviceClass:必須是介面型別,該介面型別必須加註解@WebService address:請求的url -->
	<jaxws:server id="hiServiceCXFForConfig"
		serviceClass="com.service.cxf.config.IHiServiceForCXFConfig" address="/hi">
		<!-- serviceBean:服務例項,必須是serviceClass的實現類 -->
		<jaxws:serviceBean>
			<bean class="com.service.cxf.config.HiServiceImplForCXFConfig"></bean>
		</jaxws:serviceBean>
		<!-- 請求訊息攔截器 -->
		<jaxws:inInterceptors>
			<bean id="inInterceptor2" class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
		</jaxws:inInterceptors>
		<!-- 相應訊息攔截器 -->
		<jaxws:outInterceptors>
			<bean id="outInterceptor2" class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
		</jaxws:outInterceptors>
	</jaxws:server>
</beans>

由於我們要執行在Tomcat容器中,所以,

配置完後、就要配置web.xml ,因為cxf-servlet.xml本質就是spring的配置檔案,所以在web.xml中的配置和spring的配置方式是一樣的。唯一不同的是、我們還要配置一個CXFServlet,由它來訪問我們的WebService的服務。

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0" metadata-complete="true">

	<display-name>CXF Application</display-name>
	
	<!-- 專案啟動時載入cxf配置檔案 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:com/service/cxf/config/cxf-servlet.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>cxf</servlet-name>
		<!-- CXF核心Servlet -->
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
		<!-- 指定cxf配置檔案的路徑 ,交給Spring來載入-->
<!-- 		<init-param>
			<param-name>config-location</param-name>
			<param-value>classpath:com/service/cxf/config/cxf-servlet.xml</param-value>
		</init-param> -->
	</servlet>
	<servlet-mapping>
		<servlet-name>cxf</servlet-name>
		<url-pattern>/cxf/*</url-pattern>
	</servlet-mapping>
</web-app>


點選相應的WSDL的連結、你就可以看到WSDL說明文件了。