使用apache CXF和maven开发Web Service

目前主要的java webservice框架剩下了axis2和cxf。本文对两个框架的目标、标准支持、开发和部署等方面进行了简单的对比。对于在现有web应用中发布webservice,本文建议使用cxf。 更进一步,本文介绍了cxf的嵌入式代码和web容器两种发布方式。

本文中的例子使用maven进行构建。

1 对比Axis2和CXF

jws的发布对java webservice框架产生了巨大的影响,经过大浪淘沙,目前java开发webservice的框架主要包括axis2和cxf。

axis2和cxf都是apache旗下的产品,但是其目的不同,导致webservice开发方法也不一样。两个框架都得到了开发者的支持。有必要对二者进行以下对比。

 
  Axis2 CXF
目标 WebService引擎 简易的SOA框架,可以作为ESB
ws* 标准支持 不支持WS-Policy WS-Addressing,WS-Policy, WS-RM, WS-Security,WS-I Basic Profile
数据绑定支持 XMLBeans、JiBX、JaxMe 、JaxBRI、ADB JAXB, Aegis, XMLBeans, SDO, JiBX
spring集成 不支持 支持
应用集成 困难 简单
多语言 支持C/C++ 不支持
部署 web应用 嵌入式
服务监控和管理 支持 不支持

结论:

  1. 如果希望以一种一致的方式实现webservice,特别是有跨语言的需求时,应该使用Axis2
  2. 如果需要在现有的java程序(包括web应用)中增加webservice支持,应该使用CXF

2 编写服务类

从Java6开始,WebService API从Java EE复制到了Java SE。并遵循了一系列的标准,比如JSR181(Web Service 元数据),JSR224(JAX-WS,基于XML的WebService API),JSR67(SAAJ,SOAP附件标准)等。 并分别定义到javax.jws, javax.xml.ws 和 javax.xml.soap包中。

JSR181支持使用标注(annotation)来定义WebService。在javax.jws中主要的标注类包括:

 
标注 说明
WebService 将 Java 类标记为实现 Web Service,或者将 Java 接口标记为定义 Web Service 接口
WebMethod 定制Web Service方法
WebParam 定制Web Service方法的参数
WebResult 定制Web Service方法的返回值
SOAPBinding 指定WebService的SOAP映射样式

使用标注可以在不改变代码逻辑的前提下让外部代码能够获得更多的元数据。下面就用javax.jws定义的标注来声明一个WebService:

  • 创建maven工程
mvn archetype:create -DgroupId=com.mycompany -DartifactId=cxfdemo -DarchetypeArtifactId=maven-archetype-webapp

 

  • 增加CXF依赖
<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>apache-cxf</artifactId>
        <version>${cxf.version}</version>
        <type>pom</type>
  </dependency>
  •  配置jetty插件
复制代码
<build>        
        <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
复制代码

 

  • 创建服务接口
复制代码
package cxfdemo;

import javax.jws.WebService;

@WebService
public interface CXFDemo {
     public String sayHello(String foo);
}
复制代码
  • 实现服务类
复制代码
package cxfdemo;
import javax.jws.WebService;

@WebService()
public class CXFDemoImpl implements CXFDemo {

    public String sayHello(String foo) {
        return "hello "+foo;
    }

}
复制代码

3 以endpoint发布

到目前为止,使用的都是标准Java SE中的东西。下面要开始依赖CXF实现一些功能。

首先是服务的发布。CXF不仅支持通过Web容器发布WebService,也可以在嵌入式代码中通过jetty发布WebService。

下面的测试类包含了发布服务和客户端调用的代码:

复制代码
package cxfdemo.test;

import javax.xml.ws.Endpoint;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import cxfdemo.CXFDemo;
import cxfdemo.CXFDemoImpl;

public class TestEndpoint extends TestCase {
    
    private static final String ADDRESS = "http://localhost:9000/cxfdemo"; 
    protected void setUp() throws Exception {
        super.setUp();
        
        System.out.println("Starting Server");  
        CXFDemoImpl demo = new CXFDemoImpl();  
        
        Endpoint.publish(ADDRESS, demo);
        System.out.println("Start success");
    }
    
    public void testSayHello(){
        
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(CXFDemo.class);
        factory.setAddress(ADDRESS);
        CXFDemo client = (CXFDemo)factory.create();
        Assert.assertEquals(client.sayHello("foo"), "hello foo");
    }
}
复制代码

运行测试结果如下:

复制代码
$mvn test
... ...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running cxfdemo.test.TestEndpoint
Starting Server
2012-12-12 11:29:02 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
??Ϣ: Creating Service {http://cxfdemo/}CXFDemoImplService from class cxfdemo.CXFDemo
2012-12-12 11:29:03 org.apache.cxf.endpoint.ServerImpl initDestination
??Ϣ: Setting the server's publish address to be http://localhost:9000/cxfdemo
2012-12-12 11:29:04 org.eclipse.jetty.util.log.Slf4jLog info
??Ϣ: jetty-7.4.2.v20110526
2012-12-12 11:29:04 org.eclipse.jetty.util.log.Slf4jLog info
??Ϣ: Started SelectChannelConnector@localhost:9000 STARTING
2012-12-12 11:29:04 org.eclipse.jetty.util.log.Slf4jLog info
??Ϣ: started o.e.j.s.h.ContextHandler{,null}
Start success
2012-12-12 11:29:04 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
??Ϣ: Creating Service {http://cxfdemo/}CXFDemoService from class cxfdemo.CXFDemo
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.076 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
... ...
复制代码

4 在webapp中发布

CXF提供了spring的集成,同时还提供了org.apache.cxf.transport.servlet.CXFServlet用于在web容器中发布WebService。 前面的例子中增加了整个apache-cxf的依赖,所以会自动增加对srping的引用。只需要写beans配置文件和web.xml文件即可。

  • 在web.xml中配置CXFServlet
复制代码
 <servlet>
        <servlet-name>CXFServlet</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CXFServlet</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>
复制代码

 

  • 在web.xml中增加spring的ContextLoaderListener并配置context-param
复制代码
 <context-param>
         <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/cxfdemo-beans.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
复制代码

 

  • beans配置文件内容如下

cxfdemo-beans.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"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
>
    <jaxws:endpoint id="cxfDemo" implementor="cxfdemo.CXFDemoImpl" address="/cxfdemo" />
</beans>
复制代码

 

如此,WebService就已经在web容器中发布了。启动web应用:

$mvn jetty:run

 

就可以在浏览器中看到已经发布的WebService,如下图:

Date: 2012-12-12 15:56:07 CST

Author: Holbrook

Org version 7.8.11 with Emacs version 24

Validate XHTML 1.0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于Apache CXFweb service的例子: 1. 首先,创建一个Maven项目,并添加以下依赖: ```xml <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.3.7</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.3.7</version> </dependency> </dependencies> ``` 2. 创建一个接口,并定义web service的方法: ```java @WebService public interface GreetingService { @WebMethod String sayHello(String name); } ``` 3. 创建一个实现类,并实现接口中定义的方法: ```java @Service @WebService(endpointInterface = "com.example.demo.GreetingService") public class GreetingServiceImpl implements GreetingService { @Override public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 4. 在配置文件中配置CXF: ```yaml cxf: servlet: load-on-startup: 1 url-pattern: /services/* jaxws: properties: javax.xml.ws.wsdl.service: GreetingService javax.xml.ws.wsdl.port: GreetingServicePort ``` 5. 部署应用程序并启动服务器,访问以下URL即可访问web service: ``` http://localhost:8080/services/GreetingService?wsdl ``` 6. 测试web service: ```java public class Client { public static void main(String[] args) { JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(GreetingService.class); factory.setAddress("http://localhost:8080/services/GreetingService"); GreetingService client = (GreetingService) factory.create(); String result = client.sayHello("World"); System.out.println(result); } } ``` 以上就是一个简单的基于Apache CXFweb service例子。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值