CXF WebService整合Spring
CXF WebService整合Spring
首先,CXF和spring整合需要准备如下jar包文件:
这边我是用Spring的jar包是Spring官方提供的,并没有使用CXF中的Spring的jar文件。
添加这么多文件后,首先在web.xml中添加如下配置:
<!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-server.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
然后在src目录中,新建一个applicationContext-server.xml文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans >
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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"/>
下面开始写服务器端代码,首先定制服务器端的接口,代码如下:
package com.hoo.service;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.hoo.entity.User;
import com.hoo.entity.Users;
/**
* <b>function:</b>定制客户端请求WebService所需要的接口
* @author hoojo
* @createDate 2011-3-18 上午08:22:55
* @file ComplexUserService.java
* @package com.hoo.service
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@WebService
@SOAPBinding(style = Style.RPC)
public interface IComplexUserService {
public User getUserByName(@WebParam(name = "name") String name);
public void setUser(User user);
}
下面编写WebService的实现类,服务器端实现代码如下:
package com.hoo.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import com.hoo.entity.User;
import com.hoo.entity.Users;
/**
* <b>function:</b> WebService传递复杂对象,如JavaBean、Array、List、Map等
* @author hoojo
* @createDate 2011-3-18 上午08:22:55
* @file ComplexUserService.java
* @package com.hoo.service
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ComplexUserService implements IComplexUserService {
public User getUserByName(@WebParam(name = "name") String name) {
User user = new User();
user.setId(new Date().getSeconds());
user.setName(name);
user.setAddress("china");
user.setEmail(name + "@hoo.com");
return user;
}
public void setUser(User user) {
System.out.println("############Server setUser###########");
System.out.println("setUser:" + user);
}
}
注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。
下面要在applicationContext-server.xml文件中添加如下配置:
<bean id="userServiceBean" class="com.hoo.service.ComplexUserService"/>
<bean id="inMessageInterceptor" class="com.hoo.interceptor.MessageInterceptor">
<constructor-arg value="receive"/>
</bean>
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:server id="userService" serviceClass="com.hoo.service.IComplexUserService" address="/Users">
<jaxws:serviceBean>
<!-- 要暴露的 bean 的引用 -->
<ref bean="userServiceBean"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<ref bean="inMessageInterceptor"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outLoggingInterceptor"/>
</jaxws:outInterceptors>
</jaxws:server>
下面启动tomcat服务器后,在WebBrowser中请求:
http://localhost:8080/CXFWebService/Users?wsdl
如果你能看到wsdl的xml文件的内容,就说明你成功了,注意的是上面地址的Users就是上面xml配置中的address的名称,是一一对应的。
下面编写客户端请求的代码,代码如下:
package com.hoo.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.hoo.entity.User;
import com.hoo.service.IComplexUserService;
/**
* <b>function:</b>请求Spring整合CXF的WebService客户端
* @author hoojo
* @createDate 2011-3-28 下午03:20:35
* @file SpringUsersWsClient.java
* @package com.hoo.client
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class SpringUsersWsClient {
public static void main(String[] args) {
//调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IComplexUserService.class);
factory.setAddress("http://localhost:8080/CXFWebService/Users");
IComplexUserService service = (IComplexUserService) factory.create();
System.out.println("#############Client getUserByName##############");
User user = service.getUserByName("hoojo");
System.out.println(user);
user.setAddress("China-Guangzhou");
service.setUser(user);
}
}
运行后,可以在控制台中看到
log4j:WARN No appenders could be found for logger (org.apache.cxf.bus.spring.BusApplicationContext). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 2011-3-28 18:12:26 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://service.hoo.com/}IComplexUserServiceService from class com.hoo.service.IComplexUserService #############Client getUserByName############## 27#hoojo#hoojo@hoo.com#chinaTomcat控制台
这个server端是通过Spring整合配置的,下面我们将Client端也通过Spring配置完成整合。
首先增加applicationContext-client.xml配置文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans >
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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:client id="userWsClient" serviceClass="com.hoo.service.IComplexUserService"
address="http://localhost:8080/CXFWebService/Users"/>
</beans>
客户端请求代码如下:
package com.hoo.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hoo.entity.User;
import com.hoo.service.IComplexUserService;
/**
* <b>function:</b>请求Spring整合CXF的WebService客户端
* @author hoojo
* @createDate 2011-3-28 下午03:20:35
* @file SpringUsersWsClient.java
* @package com.hoo.client
* @project CXFWebService
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class SpringUsersWsClient {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
IComplexUserService service = ctx.getBean("userWsClient", IComplexUserService.class);
System.out.println("#############Client getUserByName##############");
User user = service.getUserByName("hoojo");
System.out.println(user);
user.setAddress("China-Guangzhou");
service.setUser(user);
}
}
运行后结果如下:
#############Client getUserByName############## 45#hoojo#hoojo@hoo.com#china ############Server setUser########### setUser:45#hoojo#hoojo@hoo.com#China-Guangzhou
CXF WebService整合Spring相关推荐
- CXF WebService整合SpringMVC的maven项目
首先推荐博客:http://www.cnblogs.com/xdp-gacl/p/4259481.html http://blog.csdn.net/hu_shengyang/article/de ...
- java cxf spring_CXF整合Spring开发WebService
刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...
- CXF整合Spring开发WebService
刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...
- Spring整合CXF webservice restful 实例
webservice restful接口跟soap协议的接口实现大同小异,只是在提供服务的类/接口的注解上存在差异,具体看下面的代码,然后自己对比下就可以了. 用到的基础类 User.java [源码 ...
- 【webservice】spring整合webservice RS风格
[webservice]spring整合webservice RS风格 UserService package cn.zxl.cxf.service;import java.util.List;imp ...
- CXF系列(四):CXF整合spring
本文将介绍CXF与spring的整合开发,整体实例还是基于前三章.不过现在wsdl服务发布到tomcat,就不用发不到jetty了,相应的一些配置和代码有所改动. 1:pom.xml [plai ...
- cxf整合spring错误为:cvc-complex-type.2.4.c
cxf整合spring,报错信息如下: Multiple annotations found at this line: - cvc-complex-type.2.4.c: The matching ...
- 用cxf编写基于spring的webservice之上篇
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010741376/article/details/48517561 用CXF编写基于spring的 ...
- 《转》CXF WebService 开发文档-目录索引
CXF WebService 开发文档-目录索引 上次介绍了Axis的WebService,现在我们来看看CXF是怎么完成WebService的. Axis1 WebService 在线博文: htt ...
最新文章
- 基于Storyboard创建多分支NavigationController的方法
- 华盛顿多家博物馆因美政府停摆“闭门谢客”
- python3 解析html_Python3.x网页抓取HTMLParser
- iOS 推送手机消息背后的技术
- vs2010调试-尝试调试dll源码。
- 不想业务被中断?快来解锁华为云RDS for MySQL新特性
- 【PHP学习】—创建PHP文件(一)
- 刷爆抖音,4万好评!这本 Python 3.6 的书又断货了...
- Anaconda版本与Python版本对应关系
- 如何破解CuteFTP 4.0,CuteFTP,逆向工程技术
- 用Actix写的一个类似于Facemash的小项目总结
- Linux #! /bin/sh的意思
- NX二次开发 使用了一个已删除或无效的类号
- css里面设置一个背景边框图片或者填充一张图片,使图片填充整个div容器;
- 微信小程序之自定义组件的使用、介绍、案例分享
- Incorrect number of arguments for FUNCTION XXX
- DKHadoop的人力资源数据可视化技术框架
- s5p4418的uboot开通网络支持
- java 只获取年月日_Java获取当前时间的年月日方法
- Raspberry - 无线网卡迅捷FW150US驱动安装
热门文章
- 面向对象编程(第四篇)
- VTK:定向包围圆柱体用法实战
- JavaScript实现InsertionSort插入排序算法(附完整源码)
- boost::units模块实现使用自动前缀的简要示例
- boost::pfr::tuple_element_t相关的测试程序
- boost::hana::integral用法的测试程序
- boost::graph模块演示 GGCL Edge 接口
- boost::geometry::model::multi_point用法的测试程序
- Boost:compute::copy的复制数据测试程序
- Boost:reference wrapper参考包装的测试程序