关于Apache CXF
Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF已经是一个正式的Apache顶级项目。 Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAP、XML/HTTP、RESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTP、JMS 或者 JBI,CXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。功能特性
CXF 包含了大量的功能特性,但是主要集中在以下几个方面:支持 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。
@WebService
public interface HelloWorld {
public String sayHello(String userName);
}
@WebService(endpointInterface = "com.cxf.demo.HelloWorld", serviceName = "helloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(@WebParam(name="name")String userName) {
return "Hello," + userName;
}
}
public class Server {
public static void main(String[] args) {
System.out.println("Starting Server");
HelloWorldImpl helloWorldImpl = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld";
String content = helloWorldImpl.sayHello("allan");
Endpoint.publish(address, helloWorldImpl);
System.out.println("Start success:=="+content);
}
}
当有这样的异常:Exception in thread "main" java.lang.LinkageError: JAXB 2.1 API is being loaded from the bootstrap classloader, but this RI (from jar:file:/E:/TDDOWNLOAD/apache-cxf-2.4.3/lib/jaxb-impl-2.2.4-1.jar!/com/sun/xml/bind/v2/model/impl/ModelBuilder.class) needs 2.2 API. Use the endorsed directory mechanism to place jaxb-api.jar in the bootstrap classloader. (Seehttp://java.sun.com/j2se/1.6.0/docs/guide/standards/)
这是由于jdk的jaxb是2.1的包,而cxf的jaxb中的包为jaxb-impl-2.2.4-1.jar导致包发生冲突.
解决方案为:
则只需在jdK目录下的jre/lib/中建立endorsed文件夹,然后将jaxb-api-2.2.1.jar包与jaxb-impl-2.2.1.1.jar包拷入endorsed文件夹中,并将%JDK_HOME%\jre\lib\endorsed加入classpath后解决
一切都没有问题后,就可以发布服务:http://localhost:9000/helloWorld?wsdl ,就会出现以下内容:
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions name="helloWorld" targetNamespace="http://demo.cxf.com/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://demo.cxf.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xs:schema elementFormDefault="unqualified" targetNamespace="http://demo.cxf.com/" version="1.0" xmlns:tns="http://demo.cxf.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<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="arg0" type="xs:string" />
</xs:sequence>
</xs:complexType>
- <xs:complexType name="sayHelloResponse">
- <xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
- <wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters" />
</wsdl:message>
- <wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters" />
</wsdl:message>
- <wsdl:portType name="HelloWorld">
- <wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello" />
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="helloWorldSoapBinding" type="tns:HelloWorld">
<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="helloWorld">
- <wsdl:port binding="tns:helloWorldSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:9000/helloWorld" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
代码下载地址:http://download.csdn.net/detail/allan_chan/3936042,包含三个实例,有用了CXF新的特性:JAX-WS API等实现发布服务。