Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。
CXF 框架是一种基于 Servlet 技术的 SOA 应用开发框架,要正常运行基于 CXF 应用框架开发的企业应用,除了 CXF 框架本身之外,还需要 JDK 和 Servlet 容器的支持。
支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAP、Basic Profile、WS-Addressing、WS-Policy、WS-ReliableMessaging 和 WS-Security。Frontends:CXF 支持多种“Frontend”编程模型,CXF 实现了JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL优先开发,也支持从 Java 的代码优先开发模式。容易使用: CXF 设计得更加直观与容易使用。有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。支持二进制和遗留协议:CXF 的设计是一种可插拨的架构,既可以支持 XML ,也可以支持非 XML 的类型绑定,比如:JSON 和 CORBA。
文本框中为CXF的jar包,同时还要配置spring的jar,cxf版本尽量选择3以上,测试spring4以上版本容易和cxf3以下的版本不兼容.
备注:这里特别需要说明的是,cxf 3.0以后的版本只能在jdk1.7上使用,如果在1.6使用的话,会直接报错的。
<cxf.version>3.1.6</cxf.version>
<!-- CXF 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><!-- Jetty is needed if you're are not using the CXFServlet --><dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version></dependency><!-- End of CXF Dependencies -->
<!-- 用来声明一个servlet,CXFServlet是 --><servlet> <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup></servlet><servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/webService/*</url-pattern> </servlet-mapping>
以为cxf要依赖spring的容器,所以cxf的配置可以直接在spring.xml中配置,但是为了代码的可读性,建议新建一个cxf.xml的配置文件,在spring中import.
<import resource="classpath:cxf.xml"/>
Cxf.xml: 版本3以下的还需要import一个cxf中的soap配置文件
<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"> <!-- Import Apache CXF Bean Definition --> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<!--接口发布-->
<!--先注入实现类的bean--><bean id="demoImpl" class="com.anyuan.server.impl.HelloImpl"></bean><jaxws:endpoint id="demoService" implementor="#demoImpl" address="/demoService"/>
<!--publishedEndpointUrl可以强制修改wsdl的路径,如果未加此参数,默认地址就是 http://域名.service.demoService-->
import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;@WebService(name = "IHello",targetNamespace = "http://ehs.demo.ay.com")public interface IHello { public @WebResult(name = "result") String sayHello(@WebParam(name = "name") String name);}
import javax.jws.WebService;@WebService(targetNamespace = "http://ehs.demo.ay.com",endpointInterface = "com.anyuan.server.IHello")public class HelloImpl implements IHello { public String sayHello(String name) { return "hello with "+ name; }}
<!--@WebService中的endpointInterface 不可少,映射的是接口-->
@javax.xml.bind.annotation.XmlSchema(namespace = "http://ehs.portal.ay.com", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, attributeFormDefault = XmlNsForm.UNQUALIFIED)package com.ay.custom.demo.soap;import javax.xml.bind.annotation.XmlNsForm;
访问路径为http://localhost:8080/webService/demoService?wsdl
Wsdl如下:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ehs.demo.ay.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloImplService"targetNamespace="http://ehs.demo.ay.com">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ehs.demo.ay.com" elementFormDefault="qualified" targetNamespace="http://ehs.demo.ay.com" version="1.0">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xs:complexType name="sayHello">
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHelloResponse">
<xs:sequence>
<xs:element minOccurs="0" name="result" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="IHello">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello"></wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloImplServiceSoapBinding" type="tns:IHello">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloImplService">
<wsdl:port binding="tns:HelloImplServiceSoapBinding" name="HelloImplPort">
<soap:address location="http://localhost:8080/webService/demoService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
public static void main(String[] args) { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); String wsUrl = "http://localhost:8080/webService/demoService?wsdl";// webservice的wsdl地址 Client client = dcf.createClient(wsUrl); String method = "sayHello";// webservice的方法名 Object[] result = null; try { result = client.invoke(method, "td");// 调用webservice System.out.println(result[0]); } catch (Exception e) { e.printStackTrace(); }}
注:以下方法都需要使用cxf工具生成客户端java代码
下载网站:http://cxf.apache.org/download.html
下载后解压,设置环境变量
# set cxf environment
export CXF_HOME=/opt/soft/apache-cxf-3.1.15
export PATH=$CXF_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CXF_HOME/lib
出现如下信息表示配置成功
在命令中输入:
wsdl2java http://localhost:8080/webService/demoService?wsdl
// client第二种方法public static void main(String[] args) { HelloImplService factory=new HelloImplService(); IHello iHello=factory.getHelloImplPort(); String result = iHello.sayHello("td"); System.out.println(result);}
// client第三种方法public static void main(String[] args) { JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean(); factoryBean.getInInterceptors().add(new LoggingInInterceptor()); factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); factoryBean.setServiceClass(IHello.class); factoryBean.setAddress("http://localhost:8080/webService/demoService?wsdl"); IHello impl = (IHello) factoryBean.create(); String result = impl.sayHello("td"); System.out.println(result);}
// client第四种方法
增加cxf-client.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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:client id="client" serviceClass="com.ay.demo.ehs.IHello" address="http://localhost:8080/webService/demoService"> </jaxws:client></beans>
在spring.xml中注入
public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml"); IHello iHello= (IHello) applicationContext.getBean("client"); String result = iHello.sayHello("td"); System.out.println(result);}
官方网站:http://cxf.apache.org/
