详细介绍了在tomcat容器中创建、发布和访问CXF服务的步骤和各种方法。
一、服务器端
1.添加CXF包
1)基本包:
commons-logging-1.1.1.jar
geronimo-activation_1.1_spec-1.0.2.jar
geronimo-annotation_1.0_spec-1.1.1.jar
geronimo-javamail_1.4_spec-1.6.jar
geronimo-jaxws_2.1_spec-1.0.jar
geronimo-servlet_2.5_spec-1.2.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
jaxb-api-2.1.jar
jaxb-impl-2.1.12.jar
jetty-6.1.21.jar
jetty-util-6.1.21.jar
neethi-2.0.4.jar
saaj-api-1.3.jar
saaj-impl-1.3.2.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.8.jar
xml-resolver-1.2.jar
XmlSchema-1.4.5.jar
2)jsf和jstl(非必要,用MyEclipse创建Web Project时会自动加入,无须再添加)
jsf-api.jar
jsf-impl.jar
jstl-1.2.jar
3)cxf
cxf-2.2.4.jar
4)Spring jars,为XML Configuration添加Spring支持。
aopalliance-1.0.jar
spring-core-2.5.5.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
spring-web-2.5.5.jar
2.服务接口及实现
1)接口类,如HelloWorld.java:
package cxf.test;
import javax.jws.WebService;
@WebService
public interface HelloWorld
{
// 一个简单的方法,返回一个字符串
String say(String hello);
}
2)实现类,如HelloWorldImpl.java:
package cxf.test;
import javax.jws.WebService;
// WebService实现类.
// 使用@WebService指向Interface定义类即可.
@WebService(endpointInterface = "cxf.test.HelloWorld")
public class HelloWorldImpl implements HelloWorld
{
public String say(String hello)
{
return "hello " + hello;
}
}
3.服务配置
1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
2)beans.xml
<?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" />
<jaxws:endpoint id="webServiceHelloWorld"
address="/HelloWorld"
implementor="cxf.test.HelloWorldImpl"/>
</beans>
注:服务无需诸如index.jsp之类的配置,MyEclipse自动加入的可删除,对服务及配置无任何影响。
4.服务发布(到Tomcat)
方法一:通过MyEclipse发布
点击工具栏 图标,或者右键单击项目 → MyEclipse → Add and Remove Project Deployments。
方法二:使用Ant。
二、客户端
创建Web Project,执行下列步骤即可。无须设置web.xml。
1.添加CXF包
针对不同的方法,需要的包不尽相同。如下面“3.服务调用”的方法三,需要如下包:
commons-logging-1.1.1.jar
cxf-2.2.4.jar
neethi-2.0.4.jar
spring-core-2.5.5.jar
spring-beans-2.5.5.jar
spring-context-2.5.5.jar
wsdl4j-1.6.2.jar
XmlSchema-1.4.5.jar
2.获取服务服务接口类(类似于C/C++中的.h头文件)
方法一:直接从原项目中copy
这当然是最简单的方法,也是最“难”的方法(如果服务不是自己做的,显然没法获得)。
方法二:从wsdl文档中生成。
需要先安装cxf程序包。生成步骤如下:
1) 安装cxf,设置环境变量,如:D:\Apache\apache-cxf-2.2.4;同时,PATH后加上“;%CXF_HOME%\bin”(可选)。wsdl2java的用法如下:
wsdl2java –p 包名 –d 目录名 wsdl路径
如:wsdl2java –p demo.service.client –d e:\src htt://localhost:8080/helloWorld?wsdl
-p 指定其wsdl的命名空间,也就是要生成代码的包名
-d 指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web service的代码
-impl 生成web service的实现代码
-ant 生成build.xml文件
-compile 生成代码后编译
-quient 静默模式,不输出警告与错误信息
-all 生成所有开始端点代码:types,service proxy,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
2) 执行wsdl2java批处理程序,如:
wsdl2java -p cxf.test -d d:\src -server http://localhost:8080/CXFTomcat/services/ HelloWorld?wsdl
3) 将java接口类导入项目。
上一步生成的java类文件很多,一般的应用中只要将说明接口的那个类文件导入项目即可,如上例生成的HelloWorld.java文件。
3.服务调用
方法一:使用jws的高层封装,如:
package cxf.test;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
import cxf.test.HelloWorld; // necessary
public final class Client {
private static final QName SERVICE_NAME
= new QName("http://test.cxf/", "HelloWorld"); // 首参为接口实现类包名的反缀
private static final QName PORT_NAME
= new QName("http://test.cxf/", "HelloWorldPort");
private Client() { }
public static void main(String args[]) throws Exception {
Service service = Service.create(SERVICE_NAME);
// Endpoint Address
String endpointAddress = "http://localhost:8080/CXFTomcat/services/HelloWorld";
// Add a port to the Service
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING, endpointAddress);
HelloWorld hw = service.getPort(HelloWorld. class );
System. out .println(hw.say("World"));
}
}
方法二:使用较下层的代码更加精确的控制程序的行为,如:
package cxf.test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import cxf.test.HelloWorld; // necessary
public final class Client {
private Client() { }
public static void main(String args[]) throws Exception {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
factoryBean.getInInterceptors().add(new LoggingInInterceptor());(可选)
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());(可选)
factoryBean.setServiceClass(cxf.test.HelloWorld. class );
factoryBean.setAddress("http://localhost:8080/CXFTomcat/services/HelloWorld");
HelloWorld client = (HelloWorld)factoryBean.create();
System. out .println(client.say("God"));
System. exit (0);
}
}
备注:LoggingInInterceptor和LoggingOutInterceptor是日志拦截器,用于输入和输出时显示日志。使用与否并不影响程序的行为。
方法三:使用Spring,例如:
package cxf.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cxf.test.HelloWorld; // necessary
public final class Client {
private Client() { }
public static void main(String args[]) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( new String[] {"cxf/test/client-beans.xml"});
HelloWorld client = (HelloWorld)context.getBean("client");
String response = client.say("Joe");
System. out .println("Response: " + response);
System. exit (0);
}
}
注意:要想使用Spring来完成,在cxf.test包中必须有client-beans.xml存在,内容如下:
<?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/schema/jaxws.xsd">
<bean id="client" class="cxf.test.HelloWorld"
factory-bean="clientFactory" factory-method="create"/>
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="cxf.test.HelloWorld"/>
<property name="address" value="http://localhost:8080/CXFTomcat/services/HelloWorld"/>
</bean>
</beans>
4.执行
Run As Java Application