WebService 的CXF框架 WS方式Spring开发
1.建项目,导包.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn_itcast.maven</groupId>
<artifactId>cxf_ws_spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cxf_ws_spring</name>
<description>CXF的WS整合Spring发布</description> <dependencies>
<!-- CXF WS开发 -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</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> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- Spring整合junit开发 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9998</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
pom.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"> <!-- 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>
<!-- 欢迎页面 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> </web-app>
web.xml
2.导入实体类/service
package cn.itcast.cxf.service; import java.util.List; import javax.jws.WebMethod;
import javax.jws.WebService; import cn.itcast.cxf.domain.Car;
import cn.itcast.cxf.domain.User; @WebService
public interface IUserService {
@WebMethod
public String sayHello(String name); @WebMethod
public List<Car> findCarsByUser(User user);
}
IUIservice
package cn.itcast.cxf.service; import java.util.ArrayList;
import java.util.List; import javax.jws.WebService; import cn.itcast.cxf.domain.Car;
import cn.itcast.cxf.domain.User;
//设置endpointInterface接口服务完整类名,serviceName服务名称.
@WebService(endpointInterface = "cn.itcast.cxf.service.IUserService", serviceName = "userService")
public class UserServiceImpl implements IUserService { // 简单参数传递
public String sayHello(String name) {
return "Hello," + name;
}
// 复杂参数传递
public List<Car> findCarsByUser(User user) {
if ("xiaoming".equals(user.getUsername())) {
List<Car> cars = new ArrayList<Car>();
Car car1 = new Car();
car1.setId(1);
car1.setCarName("大众途观");
car1.setPrice(200000d);
cars.add(car1); Car car2 = new Car();
car2.setId(2);
car2.setCarName("现代ix35");
car2.setPrice(170000d);
cars.add(car2); return cars;
} else {
return null;
}
}
}
UserviceImpl
package cn.itcast.cxf.domain;
public class Car {
private Integer id;
private String carName;
private Double price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [id=" + id + ", carName=" + carName + ", price=" + price + "]";
}
}
Car
package cn.itcast.cxf.domain; import java.util.ArrayList;
import java.util.List; public class User {
private Integer id;
private String username;
private String city; private List<Car> cars = new ArrayList<Car>(); public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getCity() {
return city;
} public void setCity(String city) {
this.city = city;
} public List<Car> getCars() {
return cars;
} public void setCars(List<Car> cars) {
this.cars = cars;
} }
User
3.配置Springcxf服务发布
<?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"> <!--
address 客户端访问服务路径
serviceClass 配置接口
serviceBean 配置实现类
-->
<jaxws:server id="userService" address="/userService"
serviceClass="cn.itcast.cxf.service.IUserService">
<jaxws:serviceBean>
<bean class="cn.itcast.cxf.service.UserServiceImpl" />
</jaxws:serviceBean>
</jaxws:server> </beans>
applicationContext.xml
访问 :http://localhost:9998/cxf_ws_spring/services/userService?wsdl
4.整合Spring测试,编写客户端
1).编写applicationContext.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"> <!--
serviceClass 服务接口
address 服务访问地址
-->
<jaxws:client id="userServiceClient"
serviceClass="cn.itcast.cxf.service.IUserService"
address="http://localhost:9998/cxf_ws_spring/services/userService" >
<!-- 来源消息拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
</jaxws:inInterceptors>
<!-- 输出消息拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxws:outInterceptors>
</jaxws:client>
</beans>
客户端的配置
2)测试类编写
package cxf_ws_spring; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast.cxf.service.IUserService; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
public class JAXWS_Spring_Test {
@Autowired
private IUserService proxy; @Test
public void testCXF() {
System.out.println(proxy.sayHello("我是程序员"));
}
}
测试类
WebService 的CXF框架 WS方式Spring开发的更多相关文章
- 转载 WebService 的CXF框架 WS方式Spring开发
WebService 的CXF框架 WS方式Spring开发 1.建项目,导包. 1 <project xmlns="http://maven.apache.org/POM/4.0 ...
- WebService 的CXF框架 WS独立服务之HelloWorld
WebService:不同系统不同语言的数据交互, CXF主要分为两种服务方式: 1 )JAX-WS:传输数据, xml格式,基于SOAP协议(规范:规定了xml传递数据的编码规范) ; 2 )JAX ...
- webservice第三篇【接口开发webservice、CXF框架使用、IDEA下使用webservice、小例子】
实现接口的webservice 服务端 import javax.jws.WebService; /**面向接口的webservice发布方式 * * */ @WebService public in ...
- WebService之CXF框架
本文主要包括以下内容 ant工具的使用 利用cxf实现webservice cxf与spring整合 ajax访问webservice ant 工具 1.为什么要用到ant这个工具呢? Ant做为一种 ...
- CXF框架介绍及Spring集成
1.CXF框架概念介绍 Apache CXF 是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,这些服务可以支持多种协议,比如:SOAP.POST/HTTP.H ...
- Webservice与CXF框架快速入门
1. Webservice Webservice是一套远程调用技术规范 远程调用RPC, 实现了系统与系统进程间的远程通信.java领域有很多可实现远程通讯的技术,如:RMI(Socket + 序列化 ...
- WEBSERVICE之CXF框架开发webservice
之前学习了使用jdk开发webservice服务,现在开始学习使用框架(cxf)开发webservice. 1.准备工作 A.使用cxf开发webservice服务,需要用到apache-cxf-3. ...
- WebService中用CXF框架的wsdl部署生成客户端代码时,使用cmd命令口出现wsimport不是内部或外部命令的问题
网上有很多,都不好用,这个立竿见影的 set JAVA_HOME = *:\Program Files\Java\jdk1.8.0_181(此处为自己jdk的安装路径) set CLASSPATH = ...
- CXF整合Spring开发WebService
刚开始学webservice时就听说了cxf,一直没有尝试过,这两天试了一下,还不错,总结如下: 要使用cxf当然是要先去apache下载cxf,下载完成之后,先要配置环境变量,有以下三步: 1.打开 ...
随机推荐
- Java多线程--线程及相关的Java API
Java多线程--线程及相关的Java API 线程与进程 进程是线程的容器,程序是指令.数据的组织形式,进程是程序的实体. 一个进程中可以容纳若干个线程,线程是轻量级的进程,是程序执行的最小单位.我 ...
- Ubuntu 18.04 安装 Docker-ce(就是Docker社区版本)
一步都不用改,跟着走就行 1.更换国内软件源,推荐中国科技大学的源,稳定速度快(可选) sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak ...
- Donsen法则
“专才”对越来越少的事物了解得越来越多,直到最后他对不存在的事物无所不知: 然而,“通才”对越来越多的事物了解得越来越少,直到他对一切事物一无所知.
- 互联网自治域间IP源地址验证技术综述
一.文章信息 作者:贾溢豪,任罡,刘莹 单位:清华大学 来源:软件学报 时间:2017年 二.基于加密.签名及标记信息 2.1 技术原理 采用端到端验证的设计结构,其中以密钥协商最为常见.通信双方在事 ...
- 服务注册中心Eureka vs Zookeeper vs Consul
前言 在现在云计算和大数据快速发展的今天,业务快速发展和变化.我们以前的单一应用难以应对这种快速的变化, 因此我们需要将以前单一的大应用不断进行差分,分成若干微小的应用或者服务,这就是微服务的思想.但 ...
- POJ1811(SummerTrainingDay04-G miller-rabin判断素性 && pollard-rho分解质因数)
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 35528 Accepted: 9479 Case ...
- 【学习笔记】--- 老男孩学Python,day6 字典
详细方法:http://www.runoob.com/python/python-dictionary.html 1. dict 用大括号{} 括起来. 内部使用key:value的形式来保存数据 { ...
- 100行代码实现现代版Router
原文:http://www.html-js.com/article/JavaScript-version-100-lines-of-code-to-achieve-a-modern-version ...
- PHP获取本周的每一天的时间
1.PHP获取未来一周的时间 public function getWeek() { for($i=0;$i<7;$i++) { $arr[$i]=date('Y-m-d',strtotime( ...
- 前端开发笔记(4)css基础(下)
标签定位 相对定位 相对定位是用来微调元素位置的,让元素相对于原来的位置进行调整. <head> <meta http-equiv="Content-Type" ...