Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 容器中或者嵌入式的jetty中。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框架发布webService时,可以选择使用本地jetty(CXF内建jetty容器),也可以选择web容器。
一、使用内置的jetty发布webservice 1、pom.xml文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <properties > <cxf.version > 2.2.3</cxf.version > </properties > <dependencies > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-frontend-jaxws</artifactId > <version > ${cxf.version}</version > </dependency > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-transports-http</artifactId > <version > ${cxf.version}</version > </dependency > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-transports-http-jetty</artifactId > <version > ${cxf.version}</version > </dependency > </dependencies >
注 :CXF不仅支持通过Web容器发布WebService,也可以在嵌入式代码中通过jetty发布WebService。如果使用jetty发布webservice服务,就必须引入cxf-rt-transports-http-jetty,否则会报 Could not find destination factory for transport!
2、服务接口、实现类: 1)接口,这里使用@webService注解,来标识这是一个暴露的webservice
1 2 3 4 5 6 7 8 9 10 package cn.edu.nuc.cxfMaven;import javax.jws.WebParam;import javax.jws.WebService;@WebService public interface IHelloService { String sayHi (@WebParam(name="text" ) String text) ; }
2)实现类,通过endpoint属性值指明服务接口,在发布的时候要使用该值
1 2 3 4 5 6 7 8 9 10 11 package cn.edu.nuc.cxfMaven;import javax.jws.WebService;@WebService (endpointInterface="cn.edu.nuc.cxfMaven.IHelloService" ,serviceName="helloWorld" )public class HelloService implements IHelloService { public String sayHi (String text) { return "Hello " + text; } }
3、发布webservice服务:( endpoint和JaxWsServerFactoryBean 两种方式 ) 1)以Endpoint发布:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package cn.edu.nuc.cxfMaven;import javax.xml.ws.Endpoint;public class WebServiceApp { public static void main (String[] args) { System.out.println("web service start。。。" ); HelloService implementor= new HelloService(); String address="http://localhost:9000/helloWorld" ; Endpoint.publish(address, implementor); System.out.println("web service started。。。" ); } }
2)以JaxWsServerFactoryBean发布:
1 2 3 4 5 6 7 8 9 10 11 12 public static void main (String[] args) { JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean(); bean.setAddress("http://localhost:9000/helloWorld" ); bean.setServiceClass(HelloService.class); bean.setServiceBean(new HelloService()); bean.create(); System.out.println("server ready..." ); }
发布成功后,访问http://localhost:9000/helloWorld?wsdl 可以看到wsdl文件。
4、客户端测试: 在另外一个项目中,把上面的接口IHelloService拷贝过来(包名可以不一样,但是注解@webService必须要 );然后建立客户端类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package cxf;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class WebServiceClient { public static void main (String[] args) { JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(IHelloService.class); svr.setAddress("http://localhost:9000/helloWorld" ); IHelloService hw = (IHelloService) svr.create(); System.out.println(hw.sayHi("刘晓。。。。。。。。。" )); } }
二、使用tomcat容器发布webservice 使用web容器发布webservice时,可以借助spring(spring集成了CXF)更方便发布。
1、pom.xml文件(只需要两项)
1 2 3 4 5 6 7 8 9 10 <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-frontend-jaxws</artifactId > <version > ${cxf.version}</version > </dependency > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-transports-http</artifactId > <version > ${cxf.version}</version > </dependency >
2、服务类、实现类:
同上;
3、配置web.xml:
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 45 46 <?xml version="1.0" encoding="UTF-8"?> <web-app version ="2.5" xmlns ="http://java.sun.com/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > <context-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:application.xml</param-value > </context-param > <listener > <description > spring监听器</description > <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 > <load-on-startup > 1</load-on-startup > </servlet > <servlet-mapping > <servlet-name > CXFServlet</servlet-name > <url-pattern > /webservice/*</url-pattern > </servlet-mapping > <servlet > <servlet-name > SpringMaven</servlet-name > <servlet-class > org.springframework.web.servlet.DispatcherServlet</servlet-class > <init-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:springMVC.xml</param-value > </init-param > <load-on-startup > 1</load-on-startup > </servlet > <servlet-mapping > <servlet-name > SpringMaven</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > <session-config > <session-timeout > 30</session-timeout > </session-config > </web-app >
注: 这里,配置了springmvc和cxf两个servlet。如果只使用cxf发布webService,那么配CXFServlet即可。
4、配置application.xml:(endpoint和JaxWsServerFactoryBean 两种方式 )
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 <?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:context ="http://www.springframework.org/schema/context" xmlns:mvc ="http://www.springframework.org/schema/mvc" xmlns:tx ="http://www.springframework.org/schema/tx" 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://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.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" /> <jaxws:endpoint id ="helloWorld" implementor ="cn.edu.nuc.springMaven.services.impl.HelloService" address ="/helloWorld" /> <jaxws:server address ="/helloWorld" serviceClass ="cn.edu.nuc.springMaven.services.impl.HelloService" /> <beans >
启动tomcat成功后,可以访问 http://localhost:8080/SpringMaven/webservice/helloWorld?wsdl 来查看wsdl文件。
5、编写客户端调用webservice: 1)在另外一个项目中,把上面的接口IHelloService拷贝过来(包名可以不一样,但是注解@webService必须要);然后建立客户端类,使用JaxWsProxyFactoryBean 进行调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package cxf;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;public class WebServiceClient { public static void main (String[] args) { JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean(); svr.setServiceClass(IHelloService.class); svr.setAddress("http://localhost:8080/SpringMaven/webservice/helloWorld" ); IHelloService hw = (IHelloService) svr.create(); System.out.println(hw.sayHi("刘晓。。。。。。。。。" )); } }
2)使用spring的依赖注入,通过配置调用webService服务:方式一:在application.xml中通过jaxws:client
1 2 <jaxws:client id ="sayHello2" serviceClass ="cn.edu.nuc.springMaven.services.IHelloService" address ="http://localhost:8080/SpringMaven/webservice/helloWorld?wsdl" />
代码:
1 2 3 4 5 6 7 8 9 public class Client { public static void main (String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml" ); IHelloService clietn = (IHelloService)ctx.getBean("sayHello2" ); System.out.println(clietn.sayHi("liuxiao" )); } }
方式二:在application.xml中通过下面方式:
1 2 3 4 5 6 7 8 <bean id ="client" class ="cn.edu.nuc.springMaven.services.IHelloService" factory-bean ="clientFactory" factory-method ="create" /> <bean id ="clientFactory" class ="org.apache.cxf.jaxws.JaxWsProxyFactoryBean" > <property name ="serviceClass" value ="cn.edu.nuc.springMaven.services.IHelloService" /> <property name ="address" value ="http://localhost:8080/SpringMaven/webservice/helloWorld" /> </bean >
代码:
1 2 3 4 5 6 7 8 9 public class Client { public static void main (String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml" ); IHelloService clietn = (IHelloService)ctx.getBean("client" ); System.out.println(clietn.sayHi("liuxiao" )); } }
参考:http://haohaoxuexi.iteye.com/blog/1985995