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 ...
随机推荐
- 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 3
项目 内容 这个作业属于哪个课程 2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 实验十二 团队作业8:软件测试与Alpha冲刺 团队名称 <hello--worl ...
- 在 ServiceModel 客户端配置部分中,找不到引用协定“XXX”的默认终结点元素
一.问题 在调用远程web services接口时出现了以下问题: 二.可能的原因和解决方法 网站根目录里的web.config文件缺少了相应的配置信息 <?xml version=" ...
- Django——3 模板路径 模板变量 常用过滤器 静态文件的使用
Django 模板路径 模板变量 过滤器 静态文件的加载 模板的路径,有两种方法来使用 设置一个总的templates在大项目外面,然后在sittings的TEMPLATES中声明 在每一个APP中创 ...
- 【codeforces 796B】Find The Bone
[题目链接]:http://codeforces.com/contest/796/problem/B [题意] 一开始骨头在1号位置; 然后有m个洞,给出洞的下标; 然后有k个交换操作; 如果骨头到洞 ...
- zookeeper监控之taokeeper
1.taokeeper简介 淘宝的开源监控zookeeper的工具,年久失修! 项目地址: https://github.com/alibaba/taokeeper 监控项: CPU/MEM/LOAD ...
- 2.1.4、SparkEnv中创建BroadcastManager
Broadcast是分布式的数据共享,由BroadcastManager负责管理其创建或销毁.Broadcast一般用于处理共享的配置文件.通用Dataset.常用数据结构 通过SparkContex ...
- nyoj_8_一种排序_201311251238
一种排序 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 现在有很多长方形,每一个长方形都有一个编号,这个编号可以重复:还知道这个长方形的宽和长,编 ...
- iOS: 导航栏显示默认后退按钮
要显示系统默认的后退按钮(非自定义)的语句如下: [[self navigationController] setNavigationBarHidden:NO animated:YES];self.n ...
- java Regex
超全 http://www.rexegg.com/regex-lookarounds.html 这篇文章不错:http://www.cnblogs.com/lzq198754/p/5780340.ht ...
- Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法
对于ratings[i+1],和ratings[i]的关系有下面几种: 1. 相等.相等时ratings[i+1]相应的糖果数为1 2.ratings[i + 1] > ratings[i].在 ...