java 陣營中的Apache-cxf和C#的wcf目前是市場上webservice產品的主流框架。java
Web Service技術簡單的說就是一種帶有跨平臺特徵的,可以使得應用程序經由web形式交互的一種服務端技術。其標準依據, 不管客戶端應用所使用的語言、 平臺或內部協議是什麼, 均可以相互交換數據,執行具體的業務功能。web
web service的描述語言依據風格和標準各不相同,但表示的業務過程並不受太多的影響。目前基於soap(Simple Object Access Protocol)描述語言是xml形式的wsdl(web service define language)web service定義語言;基於REST風格則拋棄了這種方式,採用基於HTTP的資源訪問控制,並使用json交互。spring
注意,若是發佈的是web項目,jar包不易亂放,另外的話jdk版本必須是jdk1.7+apache
此外,咱們要在Tomcat下部署,須要刪除一切Jetty相關的包以及和Servlet Api衝突的jar包。json
注意:若是要和SpringMVC搭配,這個配置必須配置在SpringDispatcherServlet的前面,不然沒法攔截到請求。restful
<?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_2_5.xsd" version="2.5"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!--全部來自/*的請求,都交由 CXFServlet來處理--> <servlet> <servlet-name>HelloWorldService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>HelloWorldService</servlet-name> <url-pattern>/WebService/*</url-pattern> </servlet-mapping> </web-app>
注意,咱們若是有多個服務,只須要多配製幾個<jaxws:endpoint >便可。app
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <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" /> <!-- 用戶的Service --> <bean id="userService" class="com.tgb.service.impl.UserServiceImpl"> </bean> <bean id="helloWorld" class="com.tgb.service.impl.HelloWorldImpl"> <property name="userService" ref="userService"></property> </bean> <!-- implementor指定webservice的服務提供者 --> <!-- address爲wsdl的訪問地址 --> <jaxws:endpoint id="hello" implementor="#helloWorld" address="/helloWorld" > <!-- 添加了2個In攔截器,若是不添加攔截器,可直接註釋掉以下代碼 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean class="com.tgb.auth.AuthInterceptor"/> </jaxws:inInterceptors> <!-- 若是要配置Out攔截器,使用outInterceptors --> </jaxws:endpoint> <jaxws:endpoint id="test" implementor="#helloWorld" address="/userService" > <!-- 添加了2個In攔截器,若是不添加攔截器,可直接註釋掉以下代碼 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean class="com.tgb.auth.AuthInterceptor"/> </jaxws:inInterceptors> <!-- 若是要配置Out攔截器,使用outInterceptors --> </jaxws:endpoint> </beans>
address表示在CXFServlet下的一個分路徑,也就是說訪問 http://hostname:[端口]/WS/webService 即可以訪問到此webserice。另外也能夠注意到,一個web項目依據這種方式也能夠發佈多個web服務。框架
而後咱們訪問頁面能夠看到以下效果url
隨便點擊其中一個服務的鏈接,便能查看到wsdl的定義,這裏咱們省略。spa
5.添加restful webService支持
若是要實現RestfulService,咱們須要以下配置Application.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:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <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" /> <!-- 用戶的Service --> <bean id="userService" class="com.tgb.service.impl.UserServiceImpl"> </bean> <bean id="helloWorld" class="com.tgb.service.impl.HelloWorldImpl"> <property name="userService" ref="userService"></property> </bean> <bean id="resultSample" class="com.tgb.service.impl.MyRestfulServiceImpl"> </bean> <!-- implementor指定webservice的服務提供者 --> <!-- address爲wsdl的訪問地址 --> <jaxws:endpoint id="hello" implementor="#helloWorld" address="/helloWorld" > <!-- 添加了2個In攔截器,若是不添加攔截器,可直接註釋掉以下代碼 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean class="com.tgb.auth.AuthInterceptor"/> </jaxws:inInterceptors> <!-- 若是要配置Out攔截器,使用outInterceptors --> </jaxws:endpoint> <jaxws:endpoint id="test" implementor="#helloWorld" address="/userService" > <!-- 添加了2個In攔截器,若是不添加攔截器,可直接註釋掉以下代碼 --> <jaxws:inInterceptors> <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean class="com.tgb.auth.AuthInterceptor"/> </jaxws:inInterceptors> <!-- 若是要配置Out攔截器,使用outInterceptors --> </jaxws:endpoint> <jaxrs:server id="restful" address="/restsample"> <jaxrs:serviceBeans> <ref bean="resultSample" /> </jaxrs:serviceBeans> </jaxrs:server> </beans>
咱們就能看到以下界面