spring和cxf框架整合實現webService服務

spring和cxf框架整合實現webService服務,
1. 基於類提供的服務
第一步:需要在web.xml中配置一個servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
第二步:在一個服務類,並編輯一個方法,並在這個類上加入註解;注意:jdk必須在1.6.021以上
package com.webservice.cxf;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;


@WebService
//@BindingType(SOAPBinding.SOAP11HTTP_BINDING)//soup1協議爲默認的
@BindingType(SOAPBinding.SOAP12HTTP_BINDING) //爲soup2協議
public class HelloService {
public String sayHello(String name)
{
return "hello " + name;
}
}


第三步:在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" />
<!-- 訪問地址http://127.0.0.1/WebService_Service/ws/hello -->
<jaxws:endpoint id="helloService" address="/hello" implementor="com.webservice.cxf.HelloService">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:endpoint>
2.基於接口實現的服務
第一步:同上不在說
第二步:編輯一個接口
package com.webservice.cxf;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
@WebService
@BindingType(value=SOAPBinding.SOAP12HTTP_BINDING)
public interface HiService {
 public String sayHi(String name);
}
第三步:編輯接口的實現
package com.webservice.cxf;
public class HiServiceImpl implements HiService{
 public String sayHi(String name) {
  return "Hi " +name;
 }

第四步:配置cxf-servlet.xml
<!-- 第二種方式,基於實現接口提供的服務 serviceClass爲服務的接口 -->
<jaxws:server id="HiService" address="/hi" serviceClass="com.webservice.cxf.HiService">
 <jaxws:serviceBean><!-- 服務的實現的類 -->
  <bean class="com.webservice.cxf.HiServiceImpl"></bean>
 </jaxws:serviceBean>
 <jaxws:inInterceptors><!-- 攔截爲請求信心 -->
  <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
 </jaxws:inInterceptors>
 <jaxws:outInterceptors><!-- 攔截相應信息,這兩個攔截器可有可無 -->
  <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
 </jaxws:outInterceptors>
</jaxws:server>

 

發佈了34 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章