maven+CXF编写webservice

来源:互联网 发布:淘宝严查沙河 编辑:程序博客网 时间:2024/05/21 10:14
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。</span>

 

       该框架提供了以下功能:

  • Web 服务标准支持:CXF 支持以下 Web 服务标准:
    • Java API for XML Web Services (JAX-WS)
    • SOAP
    • Web 服务描述语言(Web Services Description Language ,WSDL)
    • 消息传输优化机制(Message Transmission Optimization Mechanism,MTOM)
    • WS-Basic Profile
    • WS-Addressing
    • WS-Policy
    • WS-ReliableMessaging
    • WS-Security
  • 前端建模:CXF 提供了前端建模的概念,允许您使用不同的前端 API 来创建 Web 服务。API 允许您使用简单的工厂 Bean 并通过 JAX-WAS 实现来创建 Web 服务。它还允许您创建动态 Web 服务客户端。
  • 工具支持:CXF 提供了用于在 Java Bean、Web 服务和 WSDL 之间进行转换的不同工具。它提供了对 Maven 和 Ant 集成的支持,并无缝地支持 Spring 集成。
  • RESTful 服务支持:CXF 支持代表性状态传输(Representational State Transfer,RESTful )服务的概念,并支持 Java 平台的 JAX-RS 实现(本系列的第 2 部分将提供有关 RESTful 服务的更多信息。)
  • 对不同传输和绑定的支持:CXF 支持不同种类的传输,从 XML 到逗号分隔值 (CSV)。除了支持 SOAP 和 HTTP 协议绑定之外,它还支持 Java Architecture for XML Binding (JAXB) 和 AEGIS 数据绑定。
  • 对非 XML 绑定的支持:CXF 支持非 XML 绑定,例如 JavaScript Object Notation (JSON) 和 Common Object Request Broker Architecture (CORBA)。它还支持 Java 业务集成(Java Business Integration,JBI)体系架构和服务组件体系架构(Service Component Architecture,SCA)。
  • code first 或者 xml first  : 支持使用code first 或者 xml first 的方式来创建web服务。


实例:

pom.xml引入依赖

<dependency> <groupId>org.apache.cxf</groupId> <artifactId>apache-cxf</artifactId> <version>3.1.7</version></dependency>

一、创建服务接口
1.创建webservice接口(客户端将实例化这个接口获取接口的实例)
import javax.jws.WebService;@WebServicepublic interface CXFDemo {    public String sayHello(String foo);}

2、创建接口的实现类

import javax.jws.WebService;@WebService()public class CXFDemoImpl implements CXFDemo {    public String sayHello(String foo) {        return "hello "+foo;    }}

3、编写测试类暴露发布服务接口

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.junit.Test;import javax.xml.ws.Endpoint;public class TestEndpoint  {    private static final String ADDRESS = "http://localhost:9000/cxfdemo";    public static void main(String[] args) {        System.out.println("Starting Server");        CXFDemoImpl demo = new CXFDemoImpl();        Endpoint.publish(ADDRESS, demo);        System.out.println("Start success");    }   @Test    public void testSayHello(){        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();        factory.setServiceClass(CXFDemo.class);        factory.setAddress(ADDRESS);        CXFDemo client = (CXFDemo)factory.create();        String respone= client.sayHello("foo");        System.out.println(respone);    }}
运行测试类main方法,打印出如下信息,服务接口就成功暴露发布了
Starting Server七月 31, 2016 3:55:32 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass信息: Creating Service {http://cxf/}CXFDemoImplService from class cxf.CXFDemo七月 31, 2016 3:55:34 下午 org.apache.cxf.endpoint.ServerImpl initDestination信息: Setting the server's publish address to be http://localhost:9000/cxfdemo七月 31, 2016 3:55:34 下午 org.eclipse.jetty.util.log.Log initialized信息: Logging initialized @4018ms七月 31, 2016 3:55:34 下午 org.eclipse.jetty.server.Server doStart信息: jetty-9.2.15.v20160210七月 31, 2016 3:55:34 下午 org.eclipse.jetty.server.handler.AbstractHandler doStart警告: No Server set for org.apache.cxf.transport.http_jetty.JettyHTTPServerEngine$1@13b3d178七月 31, 2016 3:55:34 下午 org.eclipse.jetty.server.AbstractConnector doStart信息: Started ServerConnector@27e47833{HTTP/1.1}{localhost:9000}七月 31, 2016 3:55:34 下午 org.eclipse.jetty.server.Server doStart信息: Started @4152ms七月 31, 2016 3:55:34 下午 org.eclipse.jetty.server.handler.ContextHandler setContextPath警告: Empty contextPath七月 31, 2016 3:55:34 下午 org.eclipse.jetty.server.handler.ContextHandler doStart信息: Started o.e.j.s.h.ContextHandler@34f6515b{/,null,AVAILABLE}七月 31, 2016 3:55:34 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl七月 31, 2016 3:55:35 下午 org.apache.cxf.endpoint.ServerImpl initDestination信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702七月 31, 2016 3:55:35 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImplStart success
访问 http://localhost:9000/cxfdemo?wsdl   如果看到如下xml信息就说明发布成功了



可以运行测试类中的testSayHell()方法访问接口,这个方法是模拟客户端调用服务端webservice接口,可以把这个方法提出去到另一个客户端测试类中
testSayHell方法输出如下:

七月 31, 2016 5:48:53 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass信息: Creating Service {http://cxf/}CXFDemoService from class cxf.CXFDemohello fooProcess finished with exit code 0


接口调用成功
二、将cxf与spring集成整合
创建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://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">    <jaxws:endpoint id="readerServicce2"                    implementor="cxf.CXFDemoImpl" address="/readerService2" /></beans>


以上命名空间要加上
web.xml配置
<pre name="code" class="html"><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>  <display-name>Archetype Created Web Application</display-name>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <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>/webservice/*</url-pattern>  </servlet-mapping></web-app>


再次测试一下接口是否发布成功,启动项目
访问http://localhost:8080/webservice/readerService2?wsdl  如果看到xml信息就说明发布成功了
(我用的工具是intellij idea 如果是eclipse编辑工具访问
http://localhost:8080/项目名/webservice/readerService2?wsdl 

再次运行测试类testSayHell()方法看到输出访问到服务接口返回的字符串
七月 31, 2016 5:48:53 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass信息: Creating Service {http://cxf/}CXFDemoService from class cxf.CXFDemohello fooProcess finished with exit code 0
可以把客户端配置到spring当中,在spring配置文件当中添加,要注意添加beans.xml当中的命名空间
<jaxws:client id="userWsClient" serviceClass="cxf.CXFDemo"  address="http://localhost:8080/webservice/readerService2"/>



cxf+spring整合完成
CXF 支持代表性状态传输(Representational State Transfer,RESTful )服务的概念,并支持 Java 平台的 JAX-RS 实现,接下来介绍
0 0
原创粉丝点击