共计 3159 个字符,预计需要花费 8 分钟才能阅读完成。
一、概述
Apache CXF 提供了用于方便地构建和开发 WebService 的可靠基础架构。它允许创建高性能和可扩展的服务,可以部署在 Tomcat 和基于 Spring 的轻量级容器中,也可以部署在更高级的服务器上,例如 Jboss、WebSphere 或 WebLogic。CXF 提供了以下功能:
- WebService 服务标准支持:
Java API for XML Web Services (JAX-WS)
SOAP
WebService 描述语言(Web Services Description Language,WSDL)
消息传输优化机制(Message Transmission Optimization Mechanism,MTOM)
WS-Basic Profile
WS-Addressing
WS-Policy
WS-ReliableMessaging
WS-Security
- 前端建模 :CXF 允许使用不同的前端 API 来创建 Service。如 CXF 允许使用简单的工厂 Bean 并通过 JAX-WS 实现来创建 WebService,允许创建动态 WebService 客户端。
- 工具支持 :CXF 提供了在 Java Bean、WebService 和 WSDL 之间进行转换的工具,提供了对 Maven 和 Ant 集成的支持,并无缝地支持 Spring 集成。
- RESTful 支持 :CXF 支持 Restful,并支持 Java 平台的 JAX-RS 实现。
- 对不同传输和绑定的支持 :CXF 支持不同数据类型的传输,除了支持 SOAP 和 HTTP 协议绑定外,还支持 JAXB 和 AEGIS 绑定。
- 对非 XML 绑定的支持 :CXF 支持非 XML 绑定,如 JSON、CORBA、JBI 和 SCA 等。
- Code First 和 Xml First:CXF 支持使用 Code First 或者 Xml First 的方式创建 WebService。
二、使用 CXF 内置 jetty 发布 WebService
maven:
<properties> | |
<cxf.version>3.1.4</cxf.version> | |
</properties> | |
<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> | |
<!-- 使用 cxf 内置的 jetty 服务器发布 WebService --> | |
<dependency> | |
<groupId>org.apache.cxf</groupId> | |
<artifactId>cxf-rt-transports-http-jetty</artifactId> | |
<version>${cxf.version}</version> | |
</dependency> |
—
接口:
public interface HelloService{public String helloCxf(); | |
public String hello(String name);public User getUser(int id); | |
public void saveUser(User user); | |
} |
实现:
"helloService", | (serviceName =|
endpointInterface = "cn.lg.ws.hellocxf.HelloService" | |
) | |
public class HelloServiceImpl implements HelloService{ | |
public String helloCxf(){return "Hello CXF!"; | |
} | |
public String hello(String name) | |
{return "Hello" + name; | |
} | |
public User getUser(int id) {User u1 = new User(); | |
return u1; | |
} | |
public void saveUser(User user) {System.out.println(user.toString()); | |
} | |
} |
发布:
public class PublishService{/** | |
* 使用 CXF 的 JaxWsServerFactoryBean 发布服务 | |
* @param | |
*/ | |
public static void main(String[] args) {JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean(); | |
factory.setServiceClass(HelloService.class); | |
// 服务发布地址 | |
factory.setAddress("http://localhost:8088/soap/hello"); | |
factory.setServiceBean(new HelloServiceImpl()); | |
factory.create(); | |
System.out.println("publish success"); | |
} | |
} |
使用浏览器访问 http://localhost:8088/soap/hello?wsdl 可以看到 wsdl 如下,则说明发布成功
使用 CXF 在客户端调用 WebService:
public class ClientTest{public static void main(String[] args) {JaxWsProxyFactoryBean jwpfb = new JaxWsProxyFactoryBean(); | |
jwpfb.setServiceClass(HelloService.class); | |
jwpfb.setAddress("http://localhost:8088/sop/hello"); | |
HelloService hs = (HelloService) jwpfb.create(); | |
System.out.println(hs.getUser(101)); | |
Q.p(hs.hello("luangeng")); | |
} | |
} |
—
相关类可通过以下命令产生:
wsimport -p com.ickes.cxf.client -keep http://localhost:8088/sop/hello?wsdl
使用 Spring 和 Tomcat 发布 CXF WebService 见 http://www.linuxidc.com/Linux/2017-03/141982.htm
Apache CXF 快速入门基础教程 http://www.linuxidc.com/Linux/2014-05/101383.htm
Apache CXF 实战 http://www.linuxidc.com/Linux/2012-05/59887.htm
Apache CXF 快速入门基础教程 http://www.linuxidc.com/Linux/2014-05/101383.htm
Apache CXF 拦截器 Interceptor 实现 WebServices 用户验证 http://www.linuxidc.com/Linux/2016-12/138291.htm
Apache CXF 的详细介绍 :请点这里
Apache CXF 的下载地址 :请点这里
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-03/141981.htm
