ITeye博客

将CXF整合到SSH项目

danwind · 2014-04-24 17:53 · webservice · CXF · 开源软件 · Markdown

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

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

 

 

 

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 plaincopy

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. version="2.5">
  6. <description>
  7. Web Service Test.
  8. </description>
  9. <display-name>Web Service Test</display-name>
  10. <listener>
  11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  12. </listener>
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>classpath*:applicationContext-server.xml</param-value>
  16. </context-param>
  17. <listener>
  18. <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  19. </listener>
  20. <servlet>
  21. <servlet-name>CXFService</servlet-name>
  22. <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  23. </servlet>
  24. <servlet-mapping>
  25. <servlet-name>CXFService</servlet-name>
  26. <url-pattern>/*</url-pattern>
  27. </servlet-mapping>
  28. </web-app>

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

 

 

[java] view plaincopy

  1. package com.use.test.ws.entity;
  2. public class User {
  3. int id ;
  4. String name = null;
  5. String address = null;
  6. String email = null;
  7. public int getId() {
  8. return id;
  9. }
  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 plaincopy

  1. package com.use.test.ws.server;
  2. import javax.jws.WebParam;
  3. import javax.jws.WebService;
  4. import javax.jws.soap.SOAPBinding;
  5. import javax.jws.soap.SOAPBinding.Style;
  6. import com.use.test.ws.entity.User;
  7. /**
  8. * <b>function:</b>定制客户端请求WebService所需要的接口
  9. */
  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 plaincopy

  1. package com.use.test.ws.server;
  2. import java.util.Date;
  3. import javax.jws.WebParam;
  4. import javax.jws.WebService;
  5. import javax.jws.soap.SOAPBinding;
  6. import javax.jws.soap.SOAPBinding.Style;
  7. import com.use.test.ws.entity.User;
  8. /**
  9. * <b>function:</b> 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 plaincopy

  1. <span xmlns="http://www.w3.org/1999/xhtml" style=""><span xmlns="http://www.w3.org/1999/xhtml" style=""><?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. 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">
  12. <import resource="classpath:META-INF/cxf/cxf.xml"/>
  13. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
  14. <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
  15. <bean id="userServiceBean" class="com.use.test.ws.server.TestService"/>
  16. <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
  17. <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
  18. <jaxws:server id="userService" serviceClass="com.use.test.ws.server.IService" address="/Users">
  19. <jaxws:serviceBean>
  20. <ref bean="userServiceBean"/>
  21. </jaxws:serviceBean>
  22. <jaxws:inInterceptors>
  23. <ref bean="inMessageInterceptor"/>
  24. </jaxws:inInterceptors>
  25. <jaxws:outInterceptors>
  26. <ref bean="outLoggingInterceptor"/>
  27. </jaxws:outInterceptors>
  28. </jaxws:server>
  29. </beans>
  30. </span></span><span style="color:#099000;"><span style="color:#000000;font-weight: bold;"><span style="color:#000000;font-weight: bold;"></span></span></span>

 

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 plaincopy

  1. package com.use.test.ws.client;
  2. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
  3. import com.use.test.ws.entity.User;
  4. import com.use.test.ws.server.IService;
  5. public class TestClient {
  6. public static void main(String[] args) {
  7. JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
  8. factory.setServiceClass(IService.class);
  9. 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 plaincopy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:jaxws="http://cxf.apache.org/jaxws"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. 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">
  12. <import resource="classpath:META-INF/cxf/cxf.xml"/>
  13. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
  14. <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
  15. <jaxws:client id="userWsClient" serviceClass="com.use.test.ws.server.IService" address="http://localhost:8080/ws_test/Users"/>
  16. </beans>

3.3 建立JAVA类

 

 

[java] view plaincopy

  1. package com.use.test.ws.client;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. import com.use.test.ws.entity.User;
  5. import com.use.test.ws.server.IService;
  6. /**
  7. * <b>function:</b>请求Spring整合CXF的WebService客户端
  8. */
  9. 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

 

 

在此基础上使用遇到了一些问题(很多是由于包未导入,因为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/schemas/jaxws.xsd插入xsi:schemaLocation中去。

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

解决方法:首先导入cxf的包,然后使用<import resource="classpath*:META-INF/cxf/cxf.xml"/> 

注意classpath后面需要加入*。

3.关掉 available soap services page

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

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

导致输出这个available soap services page。

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

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。