博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CXF WebService 5 整合Spring
阅读量:4132 次
发布时间:2019-05-25

本文共 6612 字,大约阅读时间需要 22 分钟。

首先,CXF和spring整合需要准备如下jar包文件:

这边我是用Spring的jar包是Spring官方提供的,并没有使用CXF中的Spring的jar文件。

添加这么多文件后,首先在web.xml中添加如下配置:

org.springframework.web.context.ContextLoaderListener
 
contextConfigLocation
classpath*:applicationContext-server.xml
 
 
org.springframework.web.util.IntrospectorCleanupListener
 
 
CXFService
org.apache.cxf.transport.servlet.CXFServlet
 
 
CXFService
/*
 

然后在src目录中,新建一个applicationContext-server.xml文件,文件内容如下:

 

 
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"

注意上面的带下划线加粗部分,这个很重要的哦!不能写错或是遗漏了。

添加完这个文件后,还需要在这个文件中导入这么几个文件。文件内容如下:

下面开始写服务器端代码,首先定制服务器端的接口,代码如下:

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;
 
/**
* function:定制客户端请求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;
 
/**
* function: 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文件中添加如下配置:

 
 
 
 
 
 
 

下面启动tomcat服务器后,在WebBrowser中请求:

如果你能看到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;
 
/**
* function:请求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配置文件,文件内容如下:

 
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"
 
 
 
address="http://localhost:8080/CXFWebService/Users"/>
 

客户端请求代码如下:

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;
 
/**
* function:请求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
你可能感兴趣的文章
从山寨Spring中学习Spring IOC原理-自动装配注解
查看>>
实例区别BeanFactory和FactoryBean
查看>>
Spring后置处理器BeanPostProcessor的应用
查看>>
Spring框架的ImportSelector到底可以干嘛
查看>>
Mysql中下划线问题
查看>>
微信小程序中使用npm过程中提示:npm WARN saveError ENOENT: no such file or directory
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
Vue项目中使用img图片和background背景图的使用方法
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>
openlayers安装引用
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>