使用cxf發布webservice總結
1、什么是webservice
Web service是一個平臺獨立的,低耦合的,自包含的、基于可編程的web的應用程序,可使用開放的XML標準來描述、發布、發現、協調和配置這些應用程序,用于開發分布式的互操作的應用程序。
2、wsdl
網絡服務描述語言是一個用來描述Web服務和說明如何與Web服務通信的XML(標準通用標記語言的子集)語言。為用戶提供詳細的接口說明書。
3、soap
簡單對象訪問協議是交換數據的一種協議規范,是一種輕量的、簡單的、基于XML(標準通用標記語言下的一個子集)的協議,它被設計成在WEB上交換結構化的和固化的信息。
4、JAX-WS
一種 Java 規范,名為 JAX-WS(JSR-224),全稱 Java API for XML-Based Web Services,可以將規范理解為官方定義的一系列接口。
5、JAX-RS
為了讓 WS 的開發與使用變得更加簡單、更加輕量級,于是出現了另一種風格的 WS,名為 JAX-RS(JSR-339),全稱 Java API for RESTful Web Services,同樣也是一種規范,同樣也有若干實現,cxf是其中比較著名的一種。
二、使用cxf發布基于soap的webservice
1、cxf與webservice的關系
剛入行的時候一直把cxf當做webservice,其實cxf只是發布調用webservice的工具而已。
2、最低maven配置
由于webservice有基于soap的實現和rest的實現,這使得cxf的maven配置比較混亂。
<!-- cxf config begin --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>2.7.3</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>2.7.3</version> </dependency> <!-- cxf config end -->
<!-- JSON begin --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.module</groupId> <artifactId>jackson-module-jaxb-annotations</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.jaxrs</groupId> <artifactId>jackson-jaxrs-json-provider</artifactId> <version>${jackson.version}</version> <!-- Jackson2.2.0 版本對于 CXF2.7.3 不支持 --> <!-- http://www.marshut.com/krrqx/fasterxml-jackson-2-2-provider-no-longer-works-with-cxf-jax-rs.pdf --> <!-- http://cxf.547215.n5.nabble.com/Creating-input-values-on-WADL-td5728910.html --> </dependency> <!-- Spring WebMVC 3.1.2 用到 --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.0</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.0</version> </dependency> <!-- JSON end -->
后面兩個依賴視你的spring版本情況而定。
3、webservice接口
package com.winssage.winssagebpm.application; import javax.jws.WebService; import com.winssage.winssagebpm.baseapplication.IBaseApplication; import com.winsssage.winssagebpm.domain.User; @WebService public interface UserApplication extends IBaseApplication<User> { public String sayHello( String name); }
4、接口實現
package com.winssage.winssagebpm.applicationImpl; import javax.inject.Named; import javax.jws.WebService; import org.springframework.transaction.annotation.Transactional; import com.winssage.winssagebpm.application.UserApplication; import com.winssage.winssagebpm.baseapplicationImpl.BaseApplicationImpl; import com.winsssage.winssagebpm.domain.User; @WebService(endpointInterface = "com.winssage.winssagebpm.application.UserApplication") @Named("userApplication") @Transactional public class UserApplicationImpl extends BaseApplicationImpl<User> implements UserApplication { public String sayHello(String name) { name = "hello" + name; return name; } }
5、spring整合cxf
#userApplication為實現中注解進來的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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.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-servlet.xml" /> <jaxws:endpoint id="security" implementor="#userApplication" address="/user" /> </beans>
6、客戶端調用已發布的webservice
<bean id="userServiceClient" class="com.winssage.winssagebpm.application.UserApplication" factory-bean="userServiceClientFactory" factory-method="create"/> <bean id="userServiceClientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="com.winssage.winssagebpm.application.UserApplication"/> <property name="address"> <value>http://localhost:9080/winssagebpm-web/api/user</value> </property> </bean>
7、查看已發布的webservice
三、使用cxf發布基于REST的webservice
詳見http://my.oschina.net/fengshuzi/blog/280408
四、總結
來自:http://my.oschina.net/fengshuzi/blog/305031
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!