1. 程式人生 > >webservice的cxf和spring整合發布

webservice的cxf和spring整合發布

.html sun ans ram 配置服務 封裝 value classpath ret

1、新建一個web項目

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

2、導入cxf相應的jar包,並部署到項目中

技術分享圖片

3、服務接口

 1 package com.xiaostudy;
 2 
 3 /**
 4  * @desc 服務器接口
 5  * @author xiaostudy
 6  *
 7  */
 8 public interface Test_service {
 9     
10     public String getNumber(String number);
11 
12 }

4、服務接口實現類

 1 package com.xiaostudy;
 2
3 import javax.jws.WebService; 4 import javax.xml.ws.BindingType; 5 import javax.xml.ws.soap.SOAPBinding; 6 7 /** 8 * @desc 服務器接口實現類 9 * @author xiaostudy 10 * 11 */ 12 @WebService 13 @BindingType(SOAPBinding.SOAP12HTTP_BINDING)//SOAP1.2聲明 14 public class Test_serviceImpl implements
Test_service { 15 16 /** 17 * @desc 處理客戶端的數據,並返回數據 18 * @param number 參數 19 * @return String 返回數據類型 20 */ 21 @Override 22 public String getNumber(String number) { 23 24 System.out.println("我服務器端執行了。。。。"); 25 26 return number+"_xiaostudy";
27 } 28 29 }

5、編寫applicationContext.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
 4     xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                             http://www.springframework.org/schema/beans/spring-beans.xsd
 7                             http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
 8                             http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
 9                             http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
10     
11     <!-- <jaxws:server發布SOAP協議的服務 ,對JaxWsServerFactoryBean類封裝-->
12     <jaxws:server address="/number" serviceClass="com.xiaostudy.Test_serviceImpl">
13         <jaxws:serviceBean>
14             <ref bean="test_serviceImpl"/>
15         </jaxws:serviceBean>
16         <!-- 配置攔截器 -->
17         <jaxws:inInterceptors>
18             <ref bean="inInterceptor"/>
19         </jaxws:inInterceptors>
20         <jaxws:outInterceptors>
21             <ref bean="outInterceptor"/>
22         </jaxws:outInterceptors>
23     </jaxws:server>    
24     
25     <!-- 配置服務實現類 -->
26     <bean name="test_serviceImpl" class="com.xiaostudy.Test_serviceImpl"/>
27     <!-- 配置攔截器的bean -->
28     <bean name="inInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
29     <bean name="outInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
30 </beans>

6、編寫web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xmlns="http://java.sun.com/xml/ns/javaee"
 4          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 5     
 6     <display-name>ws_2_cxf_spring_server</display-name>
 7     
 8     <!-- 設置spring的環境 -->
 9     <context-param>
10         <!--contextConfigLocation是不能修改的  -->
11         <param-name>contextConfigLocation</param-name>
12         <param-value>classpath:applicationContext.xml</param-value>
13     </context-param>
14     <listener>
15         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
16     </listener>
17     
18     <!-- 配置CXF的Servlet -->
19     <servlet>
20         <servlet-name>CXF</servlet-name>
21         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
22     </servlet>
23     <servlet-mapping>
24         <servlet-name>CXF</servlet-name>
25         <url-pattern>/ws/*</url-pattern>
26     </servlet-mapping>
27     
28     <welcome-file-list>
29         <welcome-file>index.html</welcome-file>
30         <welcome-file>index.htm</welcome-file>
31         <welcome-file>index.jsp</welcome-file>
32         <welcome-file>default.html</welcome-file>
33         <welcome-file>default.htm</welcome-file>
34         <welcome-file>default.jsp</welcome-file>
35     </welcome-file-list>
36 </web-app>

7、Tomcat服務開啟

技術分享圖片


webservice的cxf和spring整合發布