使用cxf发布webservice总结

来源:互联网 发布:ubuntu安装deb包打开 编辑:程序博客网 时间:2024/06/05 04:36

一、概念

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配置比较混乱。


?
1
2
3
4
5
6
7
8
9
10
11
12
<!-- 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 -->



从某种程度上说webservice发布与调用只需要添加以上两个依赖即可,但为了实现json转换我们还需要jackson相关依赖。



?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<!-- 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接口


?
1
2
3
4
5
6
7
8
9
10
11
packagecom.winssage.winssagebpm.application;
 
importjavax.jws.WebService;
 
importcom.winssage.winssagebpm.baseapplication.IBaseApplication;
importcom.winsssage.winssagebpm.domain.User;
 
@WebService
publicinterface UserApplication extendsIBaseApplication<User> {
    publicString sayHello( String name);
}



4、接口实现


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
packagecom.winssage.winssagebpm.applicationImpl;
 
importjavax.inject.Named;
importjavax.jws.WebService;
 
importorg.springframework.transaction.annotation.Transactional;
 
importcom.winssage.winssagebpm.application.UserApplication;
importcom.winssage.winssagebpm.baseapplicationImpl.BaseApplicationImpl;
importcom.winsssage.winssagebpm.domain.User;
 
@WebService(endpointInterface = "com.winssage.winssagebpm.application.UserApplication")
@Named("userApplication")
@Transactional
publicclass UserApplicationImpl extendsBaseApplicationImpl<User> implements
        UserApplication {
 
    publicString sayHello(String name) {
        name = "hello"+ name;
        returnname;
    }
 
}



5、spring整合cxf 

#userApplication为实现中注解进来的bean.


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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">
    <importresource="classpath*:META-INF/cxf/cxf.xml"/>
    <importresource="classpath*:META-INF/cxf/cxf-servlet.xml"/>
    <jaxws:endpoint id="security"implementor="#userApplication"
        address="/user"/>
</beans>

6、客户端调用已发布的webservice


?
1
2
3
4
5
6
7
8
9
<beanid="userServiceClient"class="com.winssage.winssagebpm.application.UserApplication" 
       factory-bean="userServiceClientFactory"factory-method="create"/>  
       
     <beanid="userServiceClientFactory"class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
        <propertyname="serviceClass"value="com.winssage.winssagebpm.application.UserApplication"/>  
        <propertyname="address">
            <value>http://localhost:9080/winssagebpm-web/api/user</value>
        </property>  
     </bean>



7、查看已发布的webservice


三、使用cxf发布基于REST的webservice

详见http://my.oschina.net/fengshuzi/blog/280408

四、总结


0 0