前言:好记性不如烂笔头,写博客的好处是,以前接触的东西即便忘记了,也可以从这里查找。
Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services 。可以与Spring进行快速无缝的整合。灵活的部署,可以运行在Tomcat,Jboss,Jetty(内置),IBMWS,BeaWL上面。
更多CXF介绍:http://www.ibm.com/developerworks/cn/education/java/j-cxf/
一、cxf发布服务的类用两个不同的类发布应用:
a. ServerFactoryBean -- FacotryBean
b. JaxWsServerFactoryBean(建议使用此类)
服务端与客户端类的使用映射关系如下图所示:
二、使用ServerFactoryBean类发布标准的webservice服务下载cxf安装包apache-cxf-2.4.2,里面lib下jar包以供项目所有。
1.服务端
a.新建web项目,加入cxf的jar包
b.定义webservice的接口
package com.wp.service;
import javax.jws.WebService;
@WebService
public interface HelloWs {public String sayHello(String name);
}
c.定义webservice接口的实现类
package com.wp.service;
public class HelloWsImpl implements HelloWs {@Overridepublic String sayHello(String name) {return "hello" + name;}
}
d.发布webservice服务
package com.wp.service;
import org.apache.cxf.frontend.ServerFactoryBean;
public class Server {public static void main(String[] args) {ServerFactoryBean sfb = new ServerFactoryBean();//1.服务提供者实现的接口sfb.setServiceClass(HelloWs.class);//2.指定访问路径sfb.setAddress("http://localhost:9090/ws");//3.指定服务实现类sfb.setServiceBean(new HelloWsImpl());//4.发布
sfb.create();System.out.println("发布成功...");}
}
2.客户端
a.使用wsdl2java生成客户端代码调用服务(之前有解释,这里就不做解释了)
b.使用ClientProxyFactoryBean类调用service服务
客户端必须加入cxf的jar包
浏览器访问http://localhost:9090/ws?wsdl ,查看service和operation
1) 不同项目中调用
package com.wp.test;
import com.wp.client.HelloWs;
import com.wp.client.HelloWsPortType;
public class Client {public static void main(String[] args) {HelloWs hw = new HelloWs();HelloWsPortType h = hw.getHelloWsPort();String result = h.sayHello("小强");System.out.println(result);}
}
1) 在同一项目中调用
package com.wp.service;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
public class Client {public static void main(String[] args) {ClientProxyFactoryBean cfb = new ClientProxyFactoryBean();cfb.setAddress("http://localhost:9090/ws");cfb.setServiceClass(HelloWs.class);HelloWs hw = (HelloWs) cfb.create();System.out.println(hw.sayHello("明明"));}
}
类似的例子在apache-cxf-2.4.2安装包下的samples中,开发的时候可以查看
三、使用JaxWsServerFactoryBean类发布服务1.服务端
package com.wp.service;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface HelloWs {public String sayHello(@WebParam(name="text") String text);
}
package com.wp.service;
import javax.jws.WebService;
@WebService(endpointInterface = "com.wp.service.HelloWs")
public class HelloWsImpl implements HelloWs {@Overridepublic String sayHello(String name) {return "hello " + name;}
}
package com.wp.service;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class Server {public static void main(String[] args) {JaxWsServerFactoryBean jsfb = new JaxWsServerFactoryBean();//1.服务提供者实现的接口jsfb.setServiceClass(HelloWs.class);//2.指定访问路径jsfb.setAddress("http://localhost:9090/ws");//3.指定服务实现类jsfb.setServiceBean(new HelloWsImpl());//jsfb.getInInterceptors().add(new LoggingInInterceptor());//jsfb.getOutInterceptors().add(new LoggingOutInterceptor());//4.发布
jsfb.create();System.out.println("发布成功...");}
}
2.客户端
生成客户端代码:
a.在不同项目中
package com.wp.test;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.wp.client.HelloWs;
public class Client {public static void main(String[] args) {JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();// factory.getInInterceptors().add(new LoggingInInterceptor());// factory.getOutInterceptors().add(new LoggingOutInterceptor());factory.setAddress("http://localhost:9090/ws");HelloWs client = factory.create(HelloWs.class);System.out.println(client.sayHello("World"));}
}
b.在同一项目中
package com.wp.service;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class Client {public static void main(String[] args) {JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();bean.setAddress("http://localhost:9090/ws");bean.setServiceClass(HelloWs.class);HelloWs hw = (HelloWs) bean.create();System.out.println(hw.sayHello("www"));}
}
CXF原生支持spring,可以和Spring无缝集成。本例通过tomcat启动容器来启动cxf服务。
1.新建一个maven项目(写过一个随笔),并且加入jar包
<project xmlns&#61;"http://maven.apache.org/POM/4.0.0" xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0modelVersion><groupId>com.wp.testgroupId><artifactId>cxfmavenartifactId><packaging>warpackaging><version>0.0.1-SNAPSHOTversion><name>cxfmaven Maven Webappname><url>http://maven.apache.orgurl><properties><spring.version>3.2.5.RELEASE_BUNDLEspring.version>properties><dependencies><dependency><groupId>junitgroupId><artifactId>junitartifactId><version>3.8.1version><scope>testscope>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-coreartifactId><version>${spring.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-asmartifactId><version>3.0.5.RELEASEversion>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-beansartifactId><version>${spring.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-aopartifactId><version>${spring.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-contextartifactId><version>${spring.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-expressionartifactId><version>${spring.version}version>dependency><dependency><groupId>org.springframeworkgroupId><artifactId>spring-txartifactId><version>${spring.version}version>dependency><dependency><groupId>log4jgroupId><artifactId>log4jartifactId><version>1.2.17version>dependency><dependency><groupId>org.apache.cxfgroupId><artifactId>cxf-rt-transports-httpartifactId><version>2.4.2version>dependency><dependency><groupId>commons-logginggroupId><artifactId>commons-loggingartifactId><version>1.1version>dependency><dependency><groupId>org.apache.cxfgroupId><artifactId>cxf-rt-frontend-jaxwsartifactId><version>2.4.2version>dependency><dependency><groupId>org.apache.cxfgroupId><artifactId>cxf-rt-transports-httpartifactId><version>2.4.2version>dependency><dependency><groupId>org.apache.cxfgroupId><artifactId>cxf-rt-transports-http-jettyartifactId><version>2.4.2version>dependency>dependencies><build><finalName>cxfmavenfinalName>build>
project>
2.让spring管理ServerFactoryBean
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<beansxmlns&#61;"http://www.springframework.org/schema/beans"xmlns:jaxws&#61;"http://cxf.apache.org/jaxws"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:p&#61;"http://www.springframework.org/schema/p"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd"><jaxws:server address&#61;"/helloWs"serviceClass&#61;"com.wp.service.HelloWs"><jaxws:serviceBean><bean class&#61;"com.wp.service.HelloWsImpl">bean>jaxws:serviceBean>jaxws:server>
beans>
3.cxf集成到web容器中
xml version&#61;"1.0" encoding&#61;"UTF-8"?>
<web-app version&#61;"2.5" xmlns&#61;"http://java.sun.com/xml/ns/javaee"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><display-name>display-name><welcome-file-list><welcome-file>index.jspwelcome-file>welcome-file-list><servlet><servlet-name>cxfservlet-name><servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class><init-param><param-name>config-locationparam-name><param-value>classpath:beans.xmlparam-value>init-param><load-on-startup>1load-on-startup>servlet><servlet-mapping><servlet-name>cxfservlet-name><url-pattern>/*url-pattern>servlet-mapping>
web-app>
4.启动web容器发布webservice服务
wsdl访问地址&#xff1a;http://localhost:8080/wsService/helloWs?wsdl
- 通过Eclipse中的Web Services Explorer进行测试&#xff1b;
- 建立一个客户端项目进行测试&#xff0c;跟上面一样。