CXF优点及功能
优点
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,我们可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的服务器上,例如 Jboss、IBM® WebSphere® 或 BEA WebLogic。
功能
该框架提供了以下功能[1] :
- 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)。
CXF环境搭建
在初次搭建CXF环境时,作为一个rookie的我在网上也查了很多资料。但是结果仍是一头雾水,因为不清楚什么文件要放在什么路径下,所以我把我的结构图发上来希望有所帮助。
其中:
- 目录src用来存放DAO层、服务端及工具类的接口类和实现类
- 目录WEB-INF下的lib文件夹用来存放CXF服务所依赖的包
- 配置文件applicationContext-server.xml使用 JAX-WS 前端将该服务类定义为 Spring Bean
- 配置文件web.xml用来Spring 和 CXF
因此我在开发CXF服务时,进行的步骤是:
- 将服务所依赖的包放到lib目录下。因为这个WebService的作用是查询mysql数据库中的数据,所以有个用来连接mysql数据库的jar包(mysql-connector-java-5.1.39.zip),其他所有包都可从Apache CXF官方网站下载并从解压缩后的lib目录中找到;
- 创建服务接口类,并定义一个将公开为 Web 服务的方法;
- 创建服务实现类,并将其标注为 Web 服务;
- 创建 applicationContext-server.xml;
- 创建 web.xml 。
CXF服务端测试代码
1.服务接口类:定义getNameById为Web服务的方法。
package sample.ws.cxf.service;
import javax.jws.WebService;
@WebService
public interface ISampleService {
public String getNameById(long id);
}
2.服务实现类:其中WebService注解中endpointInterface放该服务类接口的路径, serviceName指调用该接口的别名是什么,在后面的applicationContext-server.xml配置中会用到。
package sample.ws.cxf.service.impl;
import javax.jws.WebService;
import sample.ws.cxf.dao.impl.StudentDaoImpl;
import sample.ws.cxf.service.ISampleService;
@WebService(endpointInterface = "sample.ws.cxf.service.ISampleService", serviceName = "sampleService")
public class SampleServiceImpl implements ISampleService {
@Override
public String getNameById(long id) {
StudentDaoImpl std = new StudentDaoImpl();
String name = std.getNameById(id);
return "Hello: " + name;
}
}
3.DAO层接口
package sample.ws.cxf.dao;
public interface IStudentDao {
public String getNameById(long id);
}
4.DAO层实现
package sample.ws.cxf.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.jws.WebService;
import sample.ws.cxf.dao.IStudentDao;
import sample.ws.cxf.util.DBHelper;
public class StudentDaoImpl implements IStudentDao {
static String sql = null;
static DBHelper db1 = null;
static ResultSet ret = null;
@Override
public String getNameById(long id) {
sql = "select name from student where id='"+id+"'";
db1 = new DBHelper(sql);
String name = null;
try {
ret = db1.pst.executeQuery();
while(ret.next()) {
name = ret.getString(1);
}
ret.close();
db1.close();
} catch (SQLException e) {
e.printStackTrace();
}
return name;
}
}
5.工具类实现,用来连接数据库
package sample.ws.cxf.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DBHelper {
public static final String url = "jdbc:mysql://127.0.0.1/demo";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "root";
public Connection conn = null;
public PreparedStatement pst = null;
public DBHelper(String sql) {
try {
Class.forName(name);
conn = DriverManager.getConnection(url,user,password);
pst = conn.prepareStatement(sql);
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
this.conn.close();
this.pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
6.配置文件applicationContext-server.xml:文件中的< jaxws:endpoint >标记将 SampleServiceImpl Web 服务指定为 JAX-WS 端点,这实际上意味着 CXF 在内部使用 JAX-WS 来发布此 Web 服务。
<?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">
<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" />
<!-- 其中endpoint的id为SampleServiceImpl中WebService注解定义的serviceName -->
<!-- implementor指服务实现类的路径,address指在url中调用该服务的路径 -->
<jaxws:endpoint id="sampleService"
implementor="sample.ws.cxf.service.impl.SampleServiceImpl"
address="/sampleService" />
</beans>
7.配置文件web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name>CXFDemo</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-server.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 注册 CXFServlet 以处理来自客户端程序的所有请求 -->
<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>/ws/*</url-pattern>
</servlet-mapping>
</web-app>
通过tomcat运行该项目后,可以通过以下地址:http://localhost:8080/CXF-WS-Demo2/ws/sampleService?wsdl来访问该服务。其中CXF-WS-Demo2是指eclipse中的项目名称,ws是在web.xml中url-pattern处指定,sampleService是在applicationContext-server.xml中address处指定。
使用SoupUI进行测试
最后使用SoupUI来测试发布的Web Service,步骤是:
1.通过File->New SOAP Project来新建一个Project,并将http://localhost:8080/CXF-WS-Demo2/ws/sampleService?wsdl放入Initial WSDL中
2.依次点开CXF-DEMO->sampleServiceSoapBinding->getNameById->Requst 1,结果如下所示
3.最后在arg0参数处输入数据库表中的Id,运行后得到如下结果表示成功(当然前提是数据库中有数据)