关于Apache CXF
Apache CXF = Celtix + XFire,开始叫 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开发环境
CXF的官方下载地址:http://cxf.apache.org/download.html,现在目前最新版本为apache-cxf-3.1.11,集成spring4.x版本.本文案例中使用apache-cxf-2.7.18集成spring3.x以作说明.
下面是我下载下来的apache-cxf-3.1.11和apache-cxf-2.7.18版本:apache-cxf-3.1.11 apache-cxf-2.7.18
下载下来之后目录文件如下:
配置CXF的环境变量:(确保你已经配置好了JDK的环境,这里就不说明了)
新建CXF_HOME变量:G:\Tools\JAVA\cxf\apache-cxf-2.7.18
编辑classpath变量:加入%CXF_HOME%\lib;
编辑path变量:加入%CXF_HOME%\bin;
开发说明:
这里稍微写明一下本文案列的开发环境版本,由于版本的不一样会稍有影响,比如tomcat7以下的会报cxf2.17.18中servlet的错,所以这里大家都注意一下吧.
TomCat8.0 JDK1.8 eclipse-inst-win64(server 和client) CXF2.7.18
另外,案例代码风格部分稍微有点C#风格,你们可以自己写,不用和我的一样.
入门教程案例:
1.HelloWorld
由于最简单的入门案例,这里就直接先贴上代码,容易理解的东西,相信大家都看得明白.
server端:Eclipse新建一个Dynamic Web Project项目
新建IHelloService接口
package yzr.interfaces;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface IHelloService {
String sayHello(@WebParam(name="name") String name);
}
实现IHelloService的实现类HelloService
package yzr.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import yzr.interfaces.IHelloService;
@WebService(targetNamespace = "http://localhost:9000/",
portName = "HelloServicePort",
serviceName = "HelloServiceService")
@SOAPBinding(style=Style.RPC)
public class HelloService implements IHelloService {
@WebMethod(action = "sayHello")
public String sayHello(@WebParam(name="name") String name){
return "Hello, I'm "+name;
}
}
测试发布:
package yzr.main;
import javax.xml.ws.Endpoint;
import yzr.interfaces.IHelloService;
import yzr.service.HelloService;
public class run {
public static void main(String[] args) {
depolyService();
System.out.println("Server Started.......");
}
public static void depolyService(){
HelloService hs=new HelloService();
String address="http://localhost:9000/HelloWorld";
Endpoint.publish(address, hs);
}
}
打开浏览器访问:http://localhost:9000/HelloWorld ?wsdl
client端:Eclipse新建一个java project
和server端一样的接口:IHelloService
package yzr.interfaces;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface IHelloService {
String sayHello(@WebParam(name="name") String name);
}
导入jar包:
测试获取数据:
package yzr.main;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import yzr.interfaces.IHelloService;
import yzr.interfaces.IUserService;
public class app {
public static void main(String[] args) {
// 调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IHelloService.class);
factory.setAddress("http://localhost:9000/HelloWorld");
IHelloService service = (IHelloService) factory.create();
System.out.println("[result]" + service.sayHello("YZR"));
}
}
总结:在这个例子中,server端并没有用到cxf,所以server并不需要导入cxf依赖的jar包,仅仅只是使用JAX-WS规范发布了服务,而在client端中使用CXF来获取服务.
2.在server中使用CXF
此例与上面的HelloWorld不同的地方是在server中,client保持不变.
在server端中导入CXF依赖的jar包:
使用CXF发布服务:
package yzr.main;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.phase.Phase;
import yzr.intercepter.LoginInIntercepter;
import yzr.intercepter.LoginOutIntercepter;
import yzr.service.HelloService;
public class app {
public static void main(String[] args) {
depolyService();
System.out.println("服务已经发布.......");
}
public static void depolyService() {
// 发布WebService
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
// 设置Service Class
factory.setServiceClass(HelloService.class);
factory.setAddress("http://localhost:9000/HelloWorld");
// 设置ServiceBean对象
factory.setServiceBean(new HelloService());
factory.create();
System.out.println("Server start ......");
}
}
总结:使用CXF管理webservice最简单的例子就是上面这个了,发布以及获取服务,除此之外,我们还可以在服务上添加拦截器Intercepter.
3.添加Intercepter拦截器
CXF提供了AbstractPhaseInterceptor<Message>拦截器父类,往服务添加的拦截器需要继承它,重写void handleMessage(Message message)这个方法即可以编写逻辑代码.
比如LoginInIntercepter
package yzr.intercepter;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
public class LoginInIntercepter extends AbstractPhaseInterceptor<Message> {
//至少要一个带参的构造函数
public LoginInIntercepter(String phase) {
super(phase);
}
@Override
public void handleMessage(Message message) throws Fault {
System.out.println("进入LoginInIntercepter");
System.out.println(message);
/*
if (message.getDestination() != null) {
System.out.println(message.getId() + "#" + message.getDestination().getMessageObserver());
}
if (message.getExchange() != null) {
System.out.println(message.getExchange().getInMessage() + "#" + message.getExchange().getInFaultMessage());
System.out.println(message.getExchange().getOutMessage() + "#" + message.getExchange().getOutFaultMessage());
}
*/
}
}
在发布服务之前,往cxf的factory添加拦截器内容:
package yzr.main;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.phase.Phase;
import yzr.intercepter.LoginInIntercepter;
import yzr.intercepter.LoginOutIntercepter;
import yzr.service.HelloService;
public class app {
public static void main(String[] args) {
depolyService();
System.out.println("服务已经发布.......");
}
public static void depolyService() {
// 发布WebService
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
// 设置Service Class
factory.setServiceClass(HelloService.class);
factory.setAddress("http://localhost:9000/HelloWorld");
// 设置ServiceBean对象
factory.setServiceBean(new HelloService());
// 添加请求和响应的拦截器,Phase.RECEIVE只对In有效,Phase.SEND只对Out有效
factory.getInInterceptors().add(new LoginInIntercepter(Phase.RECEIVE));
factory.getOutInterceptors().add(new LoginOutIntercepter(Phase.SEND));
factory.create();
System.out.println("Server start ......");
}
}
总结:拦截器拦截有两个方向,一个是进入服务之前和执行完服务之后.通过Phase.RECEIVE和Phase.SEND指定.
4.传递复杂对象
上面使用CXF发布服务中,传递的都是字符串类型的数据,在此例中将演示CXF传递复杂对象,比如集合等类型的对象
在server端新建User对象:
package yzr.entity;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = -8645524298402587268L;
public User(){}
public User(String name){
this.name=name;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
创建IUserService服务接口:
package yzr.interfaces;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
import yzr.entity.User;
@WebService
public interface IUserService {
@WebMethod(action = "getUser")
User getUser();
@WebMethod(action = "getUsers")
List<User> getUsers();
}
实现IUserService服务接口:
package yzr.service;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import yzr.entity.User;
import yzr.interfaces.IUserService;
@WebService
@SOAPBinding(style=Style.RPC)
public class UserServiceImpl implements IUserService {
@Override
@WebMethod(action = "getUser")
public User getUser() {
return new User("YZR");
}
@Override
@WebMethod(action = "getUsers")
public List<User> getUsers() {
List<User> list=new ArrayList<User>();
list.add(new User("LYF"));
list.add(new User("YZR"));
return list;
}
}
CXF发布服务:
package yzr.main;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.phase.Phase;
import yzr.intercepter.LoginInIntercepter;
import yzr.intercepter.LoginOutIntercepter;
import yzr.service.UserServiceImpl;
public class deployUser {
public static void main(String[] args) {
// 发布WebService
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
// 设置Service Class
factory.setServiceClass(UserServiceImpl.class);
factory.setAddress("http://localhost:9000/User");
// 设置ServiceBean对象
factory.setServiceBean(new UserServiceImpl());
// 添加请求和响应的拦截器,Phase.RECEIVE只对In有效,Phase.SEND只对Out有效
factory.getInInterceptors().add(new LoginInIntercepter(Phase.RECEIVE));
factory.getOutInterceptors().add(new LoginOutIntercepter(Phase.SEND));
factory.create();
System.out.println("Server start ......");
}
}
在Client中需要User对象实体和IUserService服务接口,获取服务信息:
package yzr.main;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import yzr.interfaces.IHelloService;
import yzr.interfaces.IUserService;
public class app {
public static void main(String[] args) {
// 调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IUserService.class);
factory.setAddress("http://localhost:9000/User");
IUserService service = (IUserService) factory.create();
System.out.println("[result]" + service.getUser());
System.out.println("[List]" + service.getUsers());
/*
factory.setServiceClass(IHelloService.class);
factory.setAddress("http://localhost:9000/HelloWorld");
IHelloService service = (IHelloService) factory.create();
System.out.println("[result]" + service.sayHello("YZR"));
*/
}
}
5.CXF和Spring集成
导入依赖spring的jar包:
server端:编写server-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="ProjectManager" implementor="yzr.service.UserServiceImpl" address="http://localhost:9000/User">
</jaxws:endpoint>
</beans>
CXF发布服务:
package yzr.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class server {
public static void main(String[] args) {
new ClassPathXmlApplicationContext(new String[]{"/yzr/main/server-beans.xml"});
System.out.println("Server Started Success....");
}
}
Client端:有两种方式接收服务信息,,首先还是导入依赖Spring的jar包.
第一种方式:client-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:client id="client" serviceClass="yzr.interfaces.IUserService" address="http://localhost:9000/User"></jaxws:client>
</beans>
第二种方式:CXFClient-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="client" class="yzr.main.Client" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="yzr.interfaces.IUserService"></property>
<property name="address" value="http://localhost:9000/User"></property>
</bean>
</beans>
接收服务:
package yzr.main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import yzr.interfaces.IUserService;
public class Client {
public static void main(String[] args) {
//JAX-WS规范方式
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"/yzr/main/client-beans.xml"});
IUserService service1=(IUserService)context.getBean("client");
System.out.println(service1.getUser());
System.out.println(service1.getUsers());
//CXF方式
context=new ClassPathXmlApplicationContext(new String[]{"/yzr/main/CXFClient-beans.xml"});
IUserService service2=(IUserService)context.getBean("client");
System.out.println(service2.getUser());
System.out.println(service2.getUsers());
}
}
上面的例子中都是在main函数发布服务,接下来将演示如何将服务在tomcat中运行起来
新建applicationContext-server.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="ProjectManager" implementor="yzr.service.UserServiceImpl"
address="http://localhost:9000/User">
</jaxws:endpoint>
</beans>
修改web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>cxfService</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>
<!--
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
-->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
添加一个tomcat8.0(7以上)server,运行即可.
案例代码下载:点击