---
title: "将CXF整合到SSH项目"
author: "danwind"
date: "2014-04-24T17:53:14Z"
tags: []
categories: ["开源软件"]
slug: "cxf-ssh"
draft: false
lastmod: "2014-04-24T18:07:23Z"
id: 2054260
url: "https://danwind.iteye.com/blog/2054260"
---

在ssh项目已开发中，嵌套使用CXF  
 

1 [http://www.apache.org/](http://www.apache.org/)网站下载CXF项目。准备以下JAR包

 

![](http://dl2.iteye.com/upload/attachment/0096/4455/2c97833f-1499-3e57-abd1-b57cd590b2a4.jpg)

 

 

2 编写服务端   
2.1 新建一个空的WEB工程，我这里命名为ws\_test，将其放置于TOMCAT的webapps目录；   
2.2 然后在ws\_test下面建立WEB-INF文件夹，并在WEB-INF文件夹中建立目录lib；   
2.3 将上面提到的JAR包都拷贝到lib目录中；   
2.4 在WEB-INF目录中新建web.xml文件，并将如下内容放于其中：

 

**\[html]** [view plain](http://blog.csdn.net/fenglibing/article/details/16842587 "view plain")[copy](http://blog.csdn.net/fenglibing/article/details/16842587 "copy")

01. &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
02. &lt;web-app xmlns="http://java.sun.com/xml/ns/javaee"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"
05. version="2.5"&gt;
06. &lt;description&gt;
07. Web Service Test.
08. &lt;/description&gt;
09. &lt;display-name&gt;Web Service Test&lt;/display-name&gt;
10. &lt;listener&gt;
11. &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
12. &lt;/listener&gt;
13. &lt;context-param&gt;
14. &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
15. &lt;param-value&gt;classpath\*:applicationContext-server.xml&lt;/param-value&gt;
16. &lt;/context-param&gt;
17. &lt;listener&gt;
18. &lt;listener-class&gt;org.springframework.web.util.IntrospectorCleanupListener&lt;/listener-class&gt;
19. &lt;/listener&gt;
20. &lt;servlet&gt;
21. &lt;servlet-name&gt;CXFService&lt;/servlet-name&gt;
22. &lt;servlet-class&gt;org.apache.cxf.transport.servlet.CXFServlet&lt;/servlet-class&gt;
23. &lt;/servlet&gt;
24. &lt;servlet-mapping&gt;
25. &lt;servlet-name&gt;CXFService&lt;/servlet-name&gt;
26. &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;
27. &lt;/servlet-mapping&gt;
28. &lt;/web-app&gt;

这个配置文件是WEB应用的核心配置文件，如Listener的配置，servlet请求的映射等，这里并配置了启动的时候加载SPRING，并初使化我们的WEBSERVICE服务端配置文件。   
2.5 在ECLIPSE中新建JAVA工程，并在工程中新建lib目录，将上面提到的JAR包全部放到lib目录，并将他们全部加入到classpath中；   
2.6 增加服务端的JAVA类：

 

 

**\[java]** [view plain](http://blog.csdn.net/fenglibing/article/details/16842587 "view plain")[copy](http://blog.csdn.net/fenglibing/article/details/16842587 "copy")

01. package com.use.test.ws.entity;
02. public class User {
03. int id ;
04. String name = null;
05. String address = null;
06. String email = null;
07. public int getId() {
08. return id;
09. }
10. public void setId(int id) {
11. this.id = id;
12. }
13. public String getName() {
14. return name;
15. }
16. public void setName(String name) {
17. this.name = name;
18. }
19. public String getAddress() {
20. return address;
21. }
22. public void setAddress(String address) {
23. this.address = address;
24. }
25. public String getEmail() {
26. return email;
27. }
28. public void setEmail(String email) {
29. this.email = email;
30. }
31. }

 

**\[java]** [view plain](http://blog.csdn.net/fenglibing/article/details/16842587 "view plain")[copy](http://blog.csdn.net/fenglibing/article/details/16842587 "copy")

01. package com.use.test.ws.server;
02. import javax.jws.WebParam;
03. import javax.jws.WebService;
04. import javax.jws.soap.SOAPBinding;
05. import javax.jws.soap.SOAPBinding.Style;
06. import com.use.test.ws.entity.User;
07. /\**
08. * &lt;b&gt;function:&lt;/b&gt;定制客户端请求WebService所需要的接口
09. \*/
10. @WebService
11. @SOAPBinding(style = Style.RPC)
12. public interface IService {
13. public User getUserByName(@WebParam(name = "name") String name);
14. public void setUser(User user);
15. }

 

 

 

**\[java]** [view plain](http://blog.csdn.net/fenglibing/article/details/16842587 "view plain")[copy](http://blog.csdn.net/fenglibing/article/details/16842587 "copy")

01. package com.use.test.ws.server;
02. import java.util.Date;
03. import javax.jws.WebParam;
04. import javax.jws.WebService;
05. import javax.jws.soap.SOAPBinding;
06. import javax.jws.soap.SOAPBinding.Style;
07. import com.use.test.ws.entity.User;
08. /\**
09. * &lt;b&gt;function:&lt;/b&gt; WebService传递复杂对象，如JavaBean、Array、List、Map等
10. \*/
11. @WebService
12. @SOAPBinding(style = Style.RPC)
13. @SuppressWarnings("deprecation")
14. public class TestService implements IService {
15. public User getUserByName(@WebParam(name = "name") String name) {
16. User user = new User();
17. user.setId(new Date().getSeconds());
18. user.setName(name);
19. user.setAddress("china");
20. user.setEmail(name + "@test.com");
21. return user;
22. }
23. public void setUser(User user) {
24. System.out.println("############Server setUser###########");
25. System.out.println("setUser:" + user);
26. }
27. }

2.7 在src目录下建立applicationContext-client.xml，并放入以下内容：

 

 

**\[html]** [view plain](http://blog.csdn.net/fenglibing/article/details/16842587 "view plain")[copy](http://blog.csdn.net/fenglibing/article/details/16842587 "copy")

01. &lt;span xmlns="http://www.w3.org/1999/xhtml" style=""&gt;&lt;span xmlns="http://www.w3.org/1999/xhtml" style=""&gt;&lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:context="http://www.springframework.org/schema/context"
04. xmlns:jaxws="http://cxf.apache.org/jaxws"
05. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
08. http://www.springframework.org/schema/context
09. http://www.springframework.org/schema/context/spring-context-3.0.xsd
10. http://cxf.apache.org/jaxws
11. http://cxf.apache.org/schemas/jaxws.xsd"&gt;
12. &lt;import resource="classpath:META-INF/cxf/cxf.xml"/&gt;
13. &lt;import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/&gt;
14. &lt;import resource="classpath:META-INF/cxf/cxf-servlet.xml"/&gt;
15. &lt;bean id="userServiceBean" class="com.use.test.ws.server.TestService"/&gt;
16. &lt;bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/&gt;
17. &lt;bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/&gt;
18. &lt;jaxws:server id="userService" serviceClass="com.use.test.ws.server.IService" address="/Users"&gt;
19. &lt;jaxws:serviceBean&gt;
20. &lt;ref bean="userServiceBean"/&gt;
21. &lt;/jaxws:serviceBean&gt;
22. &lt;jaxws:inInterceptors&gt;
23. &lt;ref bean="inMessageInterceptor"/&gt;
24. &lt;/jaxws:inInterceptors&gt;
25. &lt;jaxws:outInterceptors&gt;
26. &lt;ref bean="outLoggingInterceptor"/&gt;
27. &lt;/jaxws:outInterceptors&gt;
28. &lt;/jaxws:server&gt;
29. &lt;/beans&gt;
30. &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#099000;"&gt;&lt;span style="color:#000000;font-weight: bold;"&gt;&lt;span style="color:#000000;font-weight: bold;"&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;

 

2.8 将建立的JAVA工程导出jar包，只导出src下面的内容即，并将这个jar包放到上面的TOMCAT工程中的ws\_test/WEB-INF/lib目录下，并启动TOMCAT服务器；   
2.9 启动成功后，此时访问URL：http://localhost:8080/ws\_test/Users?wsdl，应该会看到WSDL的输出。

3 编写客户端   
3.1 独立的客户端请求类

 

**\[java]** [view plain](http://blog.csdn.net/fenglibing/article/details/16842587 "view plain")[copy](http://blog.csdn.net/fenglibing/article/details/16842587 "copy")

01. package com.use.test.ws.client;
02. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
03. import com.use.test.ws.entity.User;
04. import com.use.test.ws.server.IService;
05. public class TestClient {
06. public static void main(String\[] args) {
07. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
08. factory.setServiceClass(IService.class);
09. factory.setAddress("http://localhost:8080/ws\_test/Users");
10. IService service = (IService) factory.create();
11. System.out.println("#############Client getUserByName##############");
12. User user = service.getUserByName("hoojo");
13. System.out.println(user);
14. user.setAddress("China-Guangzhou");
15. service.setUser(user);
16. }
17. }

点击右键就可以运行这个类。   
3.2 与SPRING的集成，首先需要在src下面建立配置文件applicationContext-client.xml，将输入以下内容：

 

 

**\[html]** [view plain](http://blog.csdn.net/fenglibing/article/details/16842587 "view plain")[copy](http://blog.csdn.net/fenglibing/article/details/16842587 "copy")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:context="http://www.springframework.org/schema/context"
04. xmlns:jaxws="http://cxf.apache.org/jaxws"
05. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
08. http://www.springframework.org/schema/context
09. http://www.springframework.org/schema/context/spring-context-3.0.xsd
10. http://cxf.apache.org/jaxws
11. http://cxf.apache.org/schemas/jaxws.xsd"&gt;
12. &lt;import resource="classpath:META-INF/cxf/cxf.xml"/&gt;
13. &lt;import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/&gt;
14. &lt;import resource="classpath:META-INF/cxf/cxf-servlet.xml"/&gt;
15. &lt;jaxws:client id="userWsClient" serviceClass="com.use.test.ws.server.IService" address="http://localhost:8080/ws\_test/Users"/&gt;
16. &lt;/beans&gt;

3.3 建立JAVA类

 

 

**\[java]** [view plain](http://blog.csdn.net/fenglibing/article/details/16842587 "view plain")[copy](http://blog.csdn.net/fenglibing/article/details/16842587 "copy")

01. package com.use.test.ws.client;
02. import org.springframework.context.ApplicationContext;
03. import org.springframework.context.support.ClassPathXmlApplicationContext;
04. import com.use.test.ws.entity.User;
05. import com.use.test.ws.server.IService;
06. /\**
07. * &lt;b&gt;function:&lt;/b&gt;请求Spring整合CXF的WebService客户端
08. \*/
09. public class SpringUsersWsClient {
10. public static void main(String\[] args) {
11. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
12. IService service = ctx.getBean("userWsClient", IService.class);
13. System.out.println("#############Client getUserByName##############");
14. User user = service.getUserByName("hoojo");
15. System.out.println(user);
16. user.setAddress("China-Guangzhou");
17. service.setUser(user);
18. }
19. }

 

 

直接运行这个类就OK了。

 

 

原文转载自：[http://blog.csdn.net/fenglibing/article/details/16842587](http://blog.csdn.net/fenglibing/article/details/16842587)

 

 

在此基础上使用遇到了一些问题（很多是由于包未导入，因为cxf的包实在太多）

1.The prefix "jaxws" for element "jaxws:server" is not bound.

解决方法：首先导入cxf的包，然后在头部增加xmlns:jaxws="[http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws)"

且将[http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws) [http://cxf.apache.org/schemas/jaxws.xsd](http://cxf.apache.org/schemas/jaxws.xsd)插入xsi:schemaLocation中去。

2.加载配置文件失败cxf.xml

解决方法：首先导入cxf的包，然后使用&lt;import resource="classpath\*:META-INF/cxf/cxf.xml"/&gt; 

注意classpath后面需要加入\*。

3.关掉 available soap services page

这个坑爹的问题由于缺少stax2-api-3.1.4.jar包，启动没异常，调用出异常。

再则是匹配路径导致的：例如 &lt;jaxws:server id="userService" serviceClass="org.service.IUserService" address="/webservice/user"&gt;  配置了一个地址，在web-xml文件的匹配地址/webservice/*

导致输出这个available soap services page。

CXF应该是web-xml文件的匹配地址/webservice/\*在这里找到servlet类，然后地址配置这个就可以address="user"&gt; 找到