Spring下使用开发webservice
依赖包
<!-- CXF Dependencies -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.1</version>
</dependency>
web.xml配置
web.xml中首先在contextConfigLocation中配置applicationContext-server.xml的路径,并配置CXFService,路径为/service/*,指定webservice的访问路径为localhost:8080/service/*
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:com/nanri/config/springConfig/applicationContext.xml,
classpath:com/nanri/config/webserviceConfig/applicationContext-server.xml
</param-value>
</context-param>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
applicationContext-server.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!--
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
-->
<bean id="userServiceBean" class="com.nanri.webservice.serviceTest2.ServiceImpl" /> <!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:server id="userService" serviceClass="com.nanri.webservice.serviceTest2.IService"
address="/Users">
<jaxws:serviceBean>
<!-- 要暴露的 bean 的引用 -->
<ref bean="userServiceBean" />
</jaxws:serviceBean>
</jaxws:server>
</beans>
服务器端Java类
包结构如下:

对象User
package com.nanri.webservice.serviceTest2;
public class User {
int id ;
String name = null;
String address = null;
String email = null;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
服务接口IService
package com.nanri.webservice.serviceTest2; import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface IService {
public User getUserByName(@WebParam(name = "name") String name);
public void setUser(User user);
}
服务接口实现ServiceImpl
package com.nanri.webservice.serviceTest2; import java.util.Date;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style; import org.springframework.stereotype.Repository; /**
* <b>function:</b> WebService传递复杂对象,如JavaBean、Array、List、Map等
*/
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ServiceImpl implements IService {
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 + "@test.com");
return user;
} public void setUser(User user) {
System.out.println("############Server setUser###########");
System.out.println("setUser:" + user);
}
}
客户端
SpringUsersWsClient
package com.nanri.webservice.serviceTest2;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class SpringUsersWsClient {
public static void main(String[] args) {
// 调用WebService
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IService.class);
factory.setAddress("http://localhost:8080/SpringProject/service/Users");
IService service = (IService) factory.create();
System.out.println("#############Client getUserByName##############");
User user = service.getUserByName("hoojo");
System.out.println(user);
user.setAddress("China-Guangzhou");
service.setUser(user);
}
}
访问 http://localhost:8080/SpringProject/service/Users?wsdl ,看到wsdl文件即说明配置成功。
Spring下使用开发webservice的更多相关文章
- Spring集成XFire开发WebService
Spring是眼下最流行的JavaEE Framework,可是使用Spring的Spring-WS开发WebService却十分繁琐.XFire是一个简化WebService开发的开源项目.通过Sp ...
- Spring boot+CXF开发WebService
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- Spring boot+CXF开发WebService Demo
最近工作中需要用到webservice,而且结合spring boot进行开发,参照了一些网上的资料,配置过程中出现的了一些问题,于是写了这篇博客,记录一下我这次spring boot+cxf开发的w ...
- struts1+spring+myeclipse +cxf 开发webservice以及普通java应用调用webservice的实例
Cxf + Spring+ myeclipse+ cxf 进行 Webservice服务端开发 使用Cxf开发webservice的服务端项目结构 Spring配置文件applicationCont ...
- MyEclipse下XFire开发Webservice实例
XFire Java SOAP框架概述 (摘自:http://tech.it168.com/j/e/2006-10-28/200610281432707.shtml ) MyEclipse W ...
- Spring注解+Axis2开发WebService
用Spring注解方式: 配置扫描指定包下的类 <context:component-scan base-package="包名" /> 标识类为spring管理的 ...
- Spring Boot+CXF搭建WebService(转)
概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...
- JAX-WS + Spring 开发webservice
通过几天的时间研究了下使用jax-ws来开发webservice,看了网上的一些资料总结出jax-ws的开发大概分为两种. 以下项目使用的spring3.0,jar包可以到官网下载 第一种:使用独立的 ...
- spring集成环境下的axis webservice的发布,调试
在spring集成的环境下,无论你是ssh集成,还是ssi集成的情况下,发布webservice往往在调用的时候会出错. 特别是,如果你是这个方式: 将webservice打aar包,放到tomcat ...
随机推荐
- Openssl自建CA
查看证书相关指令 # 查看公钥数字证书 openssl x509 -in cacert.pem -noout -text # 查看私钥数字证书 openssl pkcs12 -in client-ce ...
- Oracle GUID转换为String
Oracle中guid属于Raw(16)类型, 查询的时候如果不使用下面的函数, 程序中得到的是数组(byte[]). 在extjs环境下, 会带来数组的反序列化问题(newtonsoft.json) ...
- 亿级PV请求的三种负载均衡技术
在互联网+不断渗透到生活中的今天,各种各样的网络服务存在在我们身边,他们的访问流量也是大得惊人.一个大型网站(百万PV以上)想要正常访问,单单靠一台服务器是不可能提供稳定服务的.这时候就需要用负载均衡 ...
- js.ajax优缺点,工作流程
1.ajax的优点 Ajax的给我们带来的好处大家基本上都深有体会,在这里我只简单的讲几点: 1.最大的一点是页面无刷新,在页面内与服务器通信,给用户的体验非常好. 2.使用异步方式与服务器通信,不 ...
- parewise算法性能优化
在<接口自动化测试框架-AIM>这篇博客中,提到了parewise算法. 这次对其进行性能优化,共3点. 一. 因为笛卡尔积和两两拆分,是有序的. 就保证了两两拆分后的每列都是相同位置的元 ...
- sketch 相关论文
sketch 相关论文 Sketch Simplification We present a novel technique to simplify sketch drawings based on ...
- kubenetes无法创建pod/创建RC时无法自动创建pod的问题
一.问题概述 问题1: 虽然每次通过yaml创建rc都显示成功了,但是 kubectl get pod却没显示任何的pod. 问题2: 直接通过yaml创建pod提示apixxx 问题3: 通过.js ...
- nginx 定义的一些状态码
ngx_string(ngx_http_error_494_page), /* 494, request header too large */ ngx_string(ngx_http_erro ...
- RabbitMQ入门:工作队列(Work Queue)
在上一篇博客<RabbitMQ入门:Hello RabbitMQ 代码实例>中,我们通过指定的队列发送和接收消息,代码还算是比较简单的. 假设有这一些比较耗时的任务,按照上一次的那种方式, ...
- 【坚持】Selenium+Python学习记录 DAY9
2018/05/29 [来源:菜鸟教程](http://www.runoob.com/python3/python3-examples.html) 运算符重载 https://segmentfault ...