WebService 的CXF框架 WS方式Spring开发

 

1.建项目,导包.

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3 <modelVersion>4.0.0</modelVersion>
4 <groupId>cn_itcast.maven</groupId>
5 <artifactId>cxf_ws_spring</artifactId>
6 <version>0.0.1-SNAPSHOT</version>
7 <name>cxf_ws_spring</name>
8 <description>CXF的WS整合Spring发布</description>
9
10 <dependencies>
11 <!-- CXF WS开发 -->
12 <dependency>
13 <groupId>org.apache.cxf</groupId>
14 <artifactId>cxf-rt-frontend-jaxws</artifactId>
15 <version>3.0.1</version>
16 </dependency>
17 <!-- Spring开发 -->
18 <dependency>
19 <groupId>org.springframework</groupId>
20 <artifactId>spring-context</artifactId>
21 <version>4.1.7.RELEASE</version>
22 </dependency>
23
24 <dependency>
25 <groupId>org.springframework</groupId>
26 <artifactId>spring-web</artifactId>
27 <version>4.1.7.RELEASE</version>
28 </dependency>
29
30 <dependency>
31 <groupId>org.springframework</groupId>
32 <artifactId>spring-test</artifactId>
33 <version>4.1.7.RELEASE</version>
34 </dependency>
35 <!-- Spring整合junit开发 -->
36 <dependency>
37 <groupId>junit</groupId>
38 <artifactId>junit</artifactId>
39 <version>4.12</version>
40 </dependency>
41
42 </dependencies>
43 <build>
44 <plugins>
45 <plugin>
46 <groupId>org.codehaus.mojo</groupId>
47 <artifactId>tomcat-maven-plugin</artifactId>
48 <version>1.1</version>
49 <configuration>
50 <port>9998</port>
51 </configuration>
52 </plugin>
53 </plugins>
54 </build>
55 </project>
 1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xmlns="http://java.sun.com/xml/ns/javaee"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5 id="WebApp_ID" version="2.5">
6
7 <!-- spring配置文件位置 -->
8 <context-param>
9 <param-name>contextConfigLocation</param-name>
10 <param-value>classpath:applicationContext.xml</param-value>
11 </context-param>
12 <!-- spring核心监听器 -->
13 <listener>
14 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15 </listener>
16 <!-- CXF基于web访问 -->
17 <servlet>
18 <servlet-name>CXFService</servlet-name>
19 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
20 <!-- 加载级别 -->
21 <load-on-startup>1</load-on-startup>
22 </servlet>
23 <servlet-mapping>
24 <servlet-name>CXFService</servlet-name>
25 <url-pattern>/services/*</url-pattern>
26 </servlet-mapping>
27 <!-- 欢迎页面 -->
28 <welcome-file-list>
29 <welcome-file>index.html</welcome-file>
30 <welcome-file>index.htm</welcome-file>
31 <welcome-file>index.jsp</welcome-file>
32 <welcome-file>default.html</welcome-file>
33 <welcome-file>default.htm</welcome-file>
34 <welcome-file>default.jsp</welcome-file>
35 </welcome-file-list>
36
37 </web-app>

2.导入实体类/service

 1 package cn.itcast.cxf.service;
2
3 import java.util.List;
4
5 import javax.jws.WebMethod;
6 import javax.jws.WebService;
7
8 import cn.itcast.cxf.domain.Car;
9 import cn.itcast.cxf.domain.User;
10
11 @WebService
12 public interface IUserService {
13 @WebMethod
14 public String sayHello(String name);
15
16 @WebMethod
17 public List<Car> findCarsByUser(User user);
18 }
 1 package cn.itcast.cxf.service;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.jws.WebService;
7
8 import cn.itcast.cxf.domain.Car;
9 import cn.itcast.cxf.domain.User;
10 //设置endpointInterface接口服务完整类名,serviceName服务名称.
11 @WebService(endpointInterface = "cn.itcast.cxf.service.IUserService", serviceName = "userService")
12 public class UserServiceImpl implements IUserService {
13
14 // 简单参数传递
15 public String sayHello(String name) {
16 return "Hello," + name;
17 }
18 // 复杂参数传递
19 public List<Car> findCarsByUser(User user) {
20 if ("xiaoming".equals(user.getUsername())) {
21 List<Car> cars = new ArrayList<Car>();
22 Car car1 = new Car();
23 car1.setId(1);
24 car1.setCarName("大众途观");
25 car1.setPrice(200000d);
26 cars.add(car1);
27
28 Car car2 = new Car();
29 car2.setId(2);
30 car2.setCarName("现代ix35");
31 car2.setPrice(170000d);
32 cars.add(car2);
33
34 return cars;
35 } else {
36 return null;
37 }
38 }
39 }
 1 package cn.itcast.cxf.domain;
2
3 public class Car {
4 private Integer id;
5 private String carName;
6 private Double price;
7
8 public Integer getId() {
9 return id;
10 }
11
12 public void setId(Integer id) {
13 this.id = id;
14 }
15
16 public String getCarName() {
17 return carName;
18 }
19
20 public void setCarName(String carName) {
21 this.carName = carName;
22 }
23
24 public Double getPrice() {
25 return price;
26 }
27
28 public void setPrice(Double price) {
29 this.price = price;
30 }
31
32 @Override
33 public String toString() {
34 return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";
35 }
36
37 }
 1 package cn.itcast.cxf.domain;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 public class User {
7 private Integer id;
8 private String username;
9 private String city;
10
11 private List<Car> cars = new ArrayList<Car>();
12
13 public Integer getId() {
14 return id;
15 }
16
17 public void setId(Integer id) {
18 this.id = id;
19 }
20
21 public String getUsername() {
22 return username;
23 }
24
25 public void setUsername(String username) {
26 this.username = username;
27 }
28
29 public String getCity() {
30 return city;
31 }
32
33 public void setCity(String city) {
34 this.city = city;
35 }
36
37 public List<Car> getCars() {
38 return cars;
39 }
40
41 public void setCars(List<Car> cars) {
42 this.cars = cars;
43 }
44
45 }

3.配置Springcxf服务发布

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
4 xsi:schemaLocation="
5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
7
8 <!--
9 address 客户端访问服务路径
10 serviceClass 配置接口
11 serviceBean 配置实现类
12 -->
13 <jaxws:server id="userService" address="/userService"
14 serviceClass="cn.itcast.cxf.service.IUserService">
15 <jaxws:serviceBean>
16 <bean class="cn.itcast.cxf.service.UserServiceImpl" />
17 </jaxws:serviceBean>
18 </jaxws:server>
19
20 </beans>

访问 :http://localhost:9998/cxf_ws_spring/services/userService?wsdl

4.整合Spring测试,编写客户端

1).编写applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
4 xsi:schemaLocation="
5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6 http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
7
8 <!--
9 serviceClass 服务接口
10 address 服务访问地址
11 -->
12 <jaxws:client id="userServiceClient"
13 serviceClass="cn.itcast.cxf.service.IUserService"
14 address="http://localhost:9998/cxf_ws_spring/services/userService" >
15 <!-- 来源消息拦截器 -->
16 <jaxws:inInterceptors>
17 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
18 </jaxws:inInterceptors>
19 <!-- 输出消息拦截器 -->
20 <jaxws:outInterceptors>
21 <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
22 </jaxws:outInterceptors>
23 </jaxws:client>
24 </beans>

2)测试类编写

 1 package cxf_ws_spring;
2
3 import org.junit.Test;
4 import org.junit.runner.RunWith;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.test.context.ContextConfiguration;
7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8
9 import cn.itcast.cxf.service.IUserService;
10
11 @RunWith(SpringJUnit4ClassRunner.class)
12 @ContextConfiguration(locations = "classpath:applicationContext-test.xml")
13 public class JAXWS_Spring_Test {
14 @Autowired
15 private IUserService proxy;
16
17 @Test
18 public void testCXF() {
19 System.out.println(proxy.sayHello("我是程序员"));
20 }
21 }

转载 WebService 的CXF框架 WS方式Spring开发的更多相关文章

  1. WebService 的CXF框架 WS方式Spring开发

    1.建项目,导包. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...

  2. WebService 的CXF框架 WS独立服务之HelloWorld

    WebService:不同系统不同语言的数据交互, CXF主要分为两种服务方式: 1 )JAX-WS:传输数据, xml格式,基于SOAP协议(规范:规定了xml传递数据的编码规范) ; 2 )JAX ...

  3. WebService之CXF框架

    本文主要包括以下内容 ant工具的使用 利用cxf实现webservice cxf与spring整合 ajax访问webservice ant 工具 1.为什么要用到ant这个工具呢? Ant做为一种 ...

  4. webservice第三篇【接口开发webservice、CXF框架使用、IDEA下使用webservice、小例子】

    实现接口的webservice 服务端 import javax.jws.WebService; /**面向接口的webservice发布方式 * * */ @WebService public in ...

  5. Webservice与CXF框架快速入门

    1. Webservice Webservice是一套远程调用技术规范 远程调用RPC, 实现了系统与系统进程间的远程通信.java领域有很多可实现远程通讯的技术,如:RMI(Socket + 序列化 ...

  6. CXF框架介绍及Spring集成

    1.CXF框架概念介绍 Apache CXF 是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,这些服务可以支持多种协议,比如:SOAP.POST/HTTP.H ...

  7. WEBSERVICE之CXF框架开发webservice

    之前学习了使用jdk开发webservice服务,现在开始学习使用框架(cxf)开发webservice. 1.准备工作 A.使用cxf开发webservice服务,需要用到apache-cxf-3. ...

  8. WebService中用CXF框架的wsdl部署生成客户端代码时,使用cmd命令口出现wsimport不是内部或外部命令的问题

    网上有很多,都不好用,这个立竿见影的 set JAVA_HOME = *:\Program Files\Java\jdk1.8.0_181(此处为自己jdk的安装路径) set CLASSPATH = ...

  9. So easy Webservice 8.spring整合CXF 发布WS

    1.添加jar包(cxf的jar包中包含了spring的jar包),添加spring配置文件 2.web.xml中配置CXFServlet,过滤WS服务的地址 <!-- 配置CXFServlet ...

随机推荐

  1. 性能监控(5)–JAVA下的jstat命令

    jstat的基本语法为 Usage: jstat -help|-options jstat -<option> [-t] [-h<lines>] <vmid> [& ...

  2. WindowsPhone8.1 开发技巧

    1.调用系统内置滤镜: Windows.ApplicationModel.CameraApplicationManager.ShowInstalledApplicationsUI(); 2.完全退出应 ...

  3. 如何在表单中使用Ajax

    1.HTML就是一个简单表单验证,有登录按钮,点击登录会发送Ajax, 这里就是简单如果用户名为:zhouzhiruo,密码为:123456,就是登录成功,否则登录失败 应该在发送请求之前对input ...

  4. 【读书笔记】iOS-自定义URL Scheme注意事项

    如果两个不同的应用注册了同样的URL Scheme,那么后安装的应用会响应符合这种协议格式的URL. 如果你的应用的iPhone和iPad版是各自独立的(即不是Universal类型的),那么你就不应 ...

  5. 卸载Myeclipse10.5 报错“an error has occured.See the log file ...Uninstaller\...”

    找到Myeclipse的安装包,双击它,会出现Uninstaller的按钮,单击卸载即可. 另,网上说,windows下cmd命令道myeclipse.exe 目录,然后执行myeclipse.exe ...

  6. 第一个React Native程序踩到的那些坑

    毫不夸张的说用React Native写一个Hello World !程序是我碰到最复杂的Hello World.网络上的有关的环境搭建相关的文档也很多,但是总是有这样那样的问题. 官方中文版的安装文 ...

  7. Linux 操作系统下的环境变量设置

    Linux下的环境变量设置 by:授客 QQ:1033553122 1.  问题描述 linux输入命令时经常会出现提示:xxx:Command not found 2.  原因分析 Command ...

  8. jquery获取标签名,获取id

    var elementId = $(this).attr("id"); var tagName = $(this)[0].tagName;

  9. git基础介绍

    git基础介绍 这是git操作的基础篇,是以前的写的操作文档,就没有进行手打,直接把图片贴进来了,你们担待哈,有不正确的地方可以指正出来,我将在第一时间去修改,多谢哈! 一.文件状态:git系统的文件 ...

  10. URL编码:怎样读取特殊字符

    URL编码:怎样读取特殊字符 (这个我曾经谢过教程,这里整理过来)从外部文本载入到动态文本的时候,一些特殊字符(如&/%等)无法正常现实,有的符号还会导致这个符号后面的字符无法现实(如& ...