CXF-JAX-WS开发(二)spring整合CXF-JAX-WS
一、服务端
1、目录结构

2、创建maven工程[Packaging:war]

3、引入依赖
<dependencies>
<!-- CXF(这里不需要引入cxf-rt-transports-http-jetty,使用tomcat启动) -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<!-- Spring开发 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 基于spring测试开发 -->
<!-- Spring与Junit整合 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
4、配置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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 引入spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置cxf基于web访问 -->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
5、搭建服务
5.1、导入javaBean

5.1.1、domain
参考CXF-JAX-WS开发(一)入门案例,2.4.1、导入实体bean目录下的实体类Car.java和User.java
5.1.2、service
参考CXF-JAX-WS开发(一)入门案例,2.4.2、构建服务bean目录下的类IUserService.java和UserServiceImpl.java
5.2、创建spring配置文件applicationContext.xml
5.2.1、目录

5.2.2、配置
<?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">
<jaxws:server id="userService" address="/userService"
serviceClass="org.spring_cxf_ws.service.IUserService"><!-- serviceClass指定一个接口 -->
<jaxws:serviceBean>
<bean class="org.spring_cxf_ws.service.UserServiceImpl" />
</jaxws:serviceBean>
<!-- 日志配置start -->
<!-- 输入消息拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxws:inInterceptors>
<!-- 输出消息拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
<!-- 日志配置end -->
</jaxws:server>
</beans>
5.3、在pom.xml中配置tomcat插件
<build>
<plugins>
<!-- 配置tomcat端口号为: 9800 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9800</port>
</configuration>
</plugin>
</plugins>
</build>
5.4、配置jre环境1.5以上[使注解@WebService和@WebMethod生效]
参考CXF-JAX-WS开发(一)入门案例,2.4.3、配置jre环境1.5以上[使注解@WebService和@WebMethod生效]
5.5、测试服务发布是否成功
启动spring_cxf_ws,执行tomcat:run。访问:http://localhost:9800/spring_cxf_ws/services/userService?wsdl
| 名称 | 含义 |
|
|
端口号 |
| spring_cxf_ws | 项目名称 |
|
services |
web.xml中配置的servlet的url |
|
userService |
applicationContext.xml中配置的address |

二、搭建客户端
1、客户端目录结构

2、JDK的wsimport命令生成本地调用WebService服务的代码
wsimport -s . http://localhost:9800/spring_cxf_ws/services/userService?wsdl

3、创建客户端maven project[Packaing:jar]
4、引入依赖
5、复制调用WebService服务的代码到客户端工程

三、测试
1、目录结构

2、测试方案
2.1、方式一、jdk
package org.spring_cxf_ws.service; import java.util.List; /**
* 基于JDK提供的wsimport命令解析WSDL文档生成本地代码 使用本地代码生成一个代理对象,通过代理对象可以发送HTTP请求
* 请求webservice服务
*
*/
public class TestWebService_JDK {
public static void main(String[] args) {
// 方式一、jdk
IUserServiceService userService = new IUserServiceService();
IUserService proxy = userService.getIUserServicePort();
System.out.println(proxy.sayHello("张无忌"));
User user = new User();
user.setUsername("xiaoming");
List<Car> list = proxy.findCarsByUser(user);
for (Car car : list) {
System.out.println(car.getId() + ":" + car.getCarName() + ":" + car.getPrice());
}
}
}
2.2、方式二、cxf
package org.spring_cxf_ws.test; import java.util.List; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.spring_cxf_ws.service.Car;
import org.spring_cxf_ws.service.IUserService;
import org.spring_cxf_ws.service.User; /**
* 基于JDK提供的wsimport命令解析WSDL文档生成本地代码 使用本地代码生成一个代理对象,通过代理对象可以发送HTTP请求
* 请求webservice服务
*
*/
public class TestWebService_CXF {
public static void main(String[] args) {
// 方式二、cxf
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setServiceClass(IUserService.class);
jaxWsProxyFactoryBean.setAddress("http://localhost:9800/spring_cxf_ws/services/userService?wsdl");
// 创建调用远程服务的代理对象
IUserService proxy = (IUserService) jaxWsProxyFactoryBean.create();
// 调用远程服务上的sayHello方法
System.out.println(proxy.sayHello("张无忌"));
// 调用远程服务上的findCarsByUser方法
User user = new User();
user.setUsername("xiaoming");
List<Car> list = proxy.findCarsByUser(user);
for (Car car : list) {
System.out.println(car.getId() + ":" + car.getCarName() + ":" + car.getPrice());
}
}
}
2.3、方式三、spring+cxf
2.3.1、配置applicationContext-test.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">
<!--
id:唯一标识
serviceClass:服务接口的路径
address:服务地址
-->
<jaxws:client id="userServiceClient" serviceClass="org.spring_cxf_ws.service.IUserService"
address="http://localhost:9800/spring_cxf_ws/services/userService?wsdl" >
</jaxws:client>
</beans>
2.3.2、测试类
package org.spring_cxf_ws.test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.spring_cxf_ws.service.IUserService;
import org.spring_cxf_ws.service.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
public class Spring_CXF_WS_Test {
@Autowired
@Qualifier("userServiceClient")
private IUserService userService; @Test
public void testService() {
// 方式三、spring+cxf
System.out.println(userService.sayHello("张无忌"));
User user = new User();
user.setUsername("xiaoming");
System.out.println(userService.findCarsByUser(user));
}
}
CXF-JAX-WS开发(二)spring整合CXF-JAX-WS的更多相关文章
- Spring整合CXF,发布RSETful 风格WebService(转)
Spring整合CXF,发布RSETful 风格WebService 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有很大一部分都是一样的.关于发布CXF WebServer和Sp ...
- Spring整合CXF,发布RSETful 风格WebService
原文地址:http://www.cnblogs.com/hoojo/archive/2012/07/23/2605219.html 这篇文章是承接之前CXF整合Spring的这个项目示例的延伸,所以有 ...
- Spring整合CXF步骤,Spring实现webService,spring整合WebService
Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...
- Java WebService 教程系列之 Spring 整合 CXF
Java WebService 教程系列之 Spring 整合 CXF 一.引入 jar 包 <dependency> <groupId>org.apache.cxf</ ...
- Spring整合CXF之发布WebService服务
今天我们来讲下如何用Spring来整合CXF,来发布WebService服务: 给下官方文档地址:http://cxf.apache.org/docs/writing-a-service-with-s ...
- Spring整合CXF webservice restful 实例
webservice restful接口跟soap协议的接口实现大同小异,只是在提供服务的类/接口的注解上存在差异,具体看下面的代码,然后自己对比下就可以了. 用到的基础类 User.java @Xm ...
- 8、Web Service-IDEA-jaxws规范下的 spring整合CXF
前提:开发和之前eclipse的开发有很大的不同! 1.服务端的实现 1.新建项目 此时创建的是web项目 2.此时创建的项目是不完整的需要开发人员手动补充完整 3.对文件夹的设置(满满的软件使用方法 ...
- Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)
参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...
- So easy Webservice 8.spring整合CXF 发布WS
1.添加jar包(cxf的jar包中包含了spring的jar包),添加spring配置文件 2.web.xml中配置CXFServlet,过滤WS服务的地址 <!-- 配置CXFServlet ...
- Spring整合CXF发布及调用WebService
这几天终于把webService搞定,下面给大家分享一下发布webService和调用webService的方法 添加jar包 (官方下载地址:http://cxf.apache.org/downlo ...
随机推荐
- 《你说对就队》第八次团队作业:Alpha冲刺 第五天
<你说对就队>第八次团队作业:Alpha冲刺 第五天 项目 内容 这个作业属于哪个课程 [教师博客主页链接] 这个作业的要求在哪里 [作业链接地址] 团队名称 <你说对就队> ...
- .Net防sql注入的方法总结
#防sql注入的常用方法: 1.服务端对前端传过来的参数值进行类型验证: 2.服务端执行sql,使用参数化传值,而不要使用sql字符串拼接: 3.服务端对前端传过来的数据进行sql关键词过来与检测: ...
- 洛谷 P1308 统计单词数【字符串处理】
题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给 ...
- seleniumd对象的操作方法,属性,keys
这是通过find方法找到的页面元素,此对象提供了多种方法,让我们可以与页面元素进行交互,例如点击.清空. 目录: 1. 方法 2. 属性 3. keys 方法 clear()清空 如果当前元素中有文本 ...
- 使用Eclipse 创建Spring Boot项目
一.为什么要使用 Spring Boot ? Spring Boot解决的问题 (1) Spring Boot使编码变简单 (2) Spring Boot使配置变简单 (3) Spring Boot使 ...
- (45). Spring Boot MyBatis连接Mysql数据库【从零开始学Spring Boot】
大家在开发的时候,会喜欢jdbcTemplate操作数据库,有喜欢JPA操作数据库的,有喜欢MyBatis操作数据库的,对于这些我个人觉得哪个使用顺手就使用哪个就好了,并没有一定要使用哪个,个人在实际 ...
- Spring整体架构
Spring整体架构 Spring的整体架构 Spring框架是分层架构的,它包含了一系列的功能要素. Spring整体架构图 模块分类 1. Core Container Core Containe ...
- 依据矩阵的二维相关系数进行OCR识别
我想通过简单的模板匹配来进行图像识别. 把预处理好的字符图片,分别与A到Z的样本图片进行模板匹配. 结果最大的表明相关性最大,就能够识别字符图片了. 在实际应用中.我用了openCV的matchTem ...
- Oracle 堵塞(blocking blocked)
堵塞是DBA常常碰到的情形,尤其是不良的应用程序设计的堵塞将导致性能严重下降直至数据库崩溃. 对DBA而言,有必要知道怎样定位到当前系统有哪些堵塞,究竟谁是堵塞者,谁是被堵塞者.本文对此给出了描写叙述 ...
- Ext.tree.Panel实现单选,多选
Extjs var productCategoryTreeLookUpFn = function(callback) { var productCategoryLookUpWindow; var pr ...