做项目时,涉及到多个异构系统的交互,采用了Apache CXF,下面通过实例演示Apache CXF 2.7与Spring集成,发布和调用Web Service。
环境:
win8 64位
tomcat 7 64位
jdk 7 64位
cxf 2.7
spring 3
项目结构
1、在同一个项目中模仿服务端和客户端(实际项目中是不可能的,但是客户端却依赖于服务端的Web Service接口,那么可以通过导出jar的方式)
类库
在apache cxf 官网下载cxf2.7,解压,在lib下即可找到这些jar
几个类的代码
package com.test.server;
import javax.jws.WebService;
@WebService
public interface IHelloWorldServer {
public String sayHello(String username);
}
package com.test.server;
import javax.jws.WebService;
@WebService(endpointInterface = "com.test.server.IHelloWorldServer", serviceName = "HelloService")
public class HelloWorldServerImp implements IHelloWorldServer{
@Override
public String sayHello(String username) {
return username+" : HelloWorld";
}
}
package com.test.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.server.IHelloWorldServer;
public class HelloWorldClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
IHelloWorldServer helloService = (IHelloWorldServer) context.getBean("client");
String response = helloService.sayHello("jialin");
System.out.println(response);
}
}
相应的配置文件
applicationContext-server.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">
<!--
***注意***
手动添加的内容:
xmlns:jaxws="http://cxf.apache.org/jaxws"
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="helloService" implementor="com.test.server.HelloWorldServerImp" address="/helloService" />
</beans>
applicationContext-client.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">
<!--
***注意***
手动添加的内容:
xmlns:jaxws="http://cxf.apache.org/jaxws"
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" />
<bean id="client" class="com.test.server.IHelloWorldServer" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.test.server.IHelloWorldServer" />
<property name="address" value="http://localhost:8080/cxf_demo/ws/helloService" />
</bean>
</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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>cxf_demo</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>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
配置完成后,部署到tomcat,成功启动后。
在浏览器中输入
http://localhost:8080/cxf_demo/ws
说明web service发布成功,可以点击进入查看相应的WSDL.
这时运行客户端得到如下结果
以上是在同一个项目中模仿客户端和服务端。下面再看一个服务和客户端不在同一个项目中的,这是符合现实的。
2、
服务端
与上述项目结构一样,只是没有com.test.client这个包和applicationContext-client.xml
客户端
新建一个普通的java项目即可,项目结构:
注意cxf-serverinterface这个jar,是到处的服务端的接口即为com.test.server.IHelloWorld
HelloWorldClient.java
package com.test.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.server.IHelloWorldServer;
public class HelloWorldClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");
IHelloWorldServer helloService = (IHelloWorldServer) context.getBean("client");
String response = helloService.sayHello("jialin");
System.out.println(response);
}
}
applicationContext-client.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">
<!--
***注意***
手动添加的内容:
xmlns:jaxws="http://cxf.apache.org/jaxws"
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" />
<bean id="client" class="com.test.server.IHelloWorldServer" factory-bean="clientFactory" factory-method="create" />
<bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="com.test.server.IHelloWorldServer" />
<property name="address" value="http://localhost:8080/cxf_demo/ws/helloService" />
</bean>
</beans>
部署服务端到tomcat,启动。
启动Web Service服务,然后访问http://localhost:8080/cxf_demo/ws地址来确定web service启动正确。
运行Client测试类,运行结果:
成功!