Spring Boot 开发 WebService 服务
WebService 虽然现在大部分互联网企业不太提倡使用,但在以第三方接口为主导的市场,对方来什么接口你还得用什么接口,不可能把接口重写了。例如大部分传统的大型企业都在用 WebService,并且版本还不一样。
本章主要介绍在 Spring Boot 下有常用的整合 WebService 的方法并给出示例。为了方便测试,本章有两个独立的项目
- 用户的获取、增加、更新、删除 webservice 服务
- 用于调用 1 的webservice 服务的客户端
1 新建 Spring Boot Maven 示例工程项目
注意:是用来 IDEA 开发工具
- File > New > Project,如下图选择
Spring Initializr
然后点击 【Next】下一步 - 填写
GroupId
(包名)、Artifact
(项目名) 即可。点击 下一步
groupId=com.fishpro
artifactId=webservice - 选择依赖
Spring Web Starter
前面打钩。 - 项目名设置为
spring-boot-study-webservice
.
2 引入依赖 Pom.xml
这里主要是引入 org.apache.cxf
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-spring-boot-starter-jaxws -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3 编写一个用户获取、新增、修改、删除服务
3.1 传输对象 UserDto
通常我们把展示层与服务层之间传输的对象使用Dto后缀来标识。
UserDto(路径 src/main/java/com/fishpro/webservice/dto/UserDto.java)
/**
* 用户传输实体对象 通常我们把展示层与服务层之间传输的对象使用Dto后缀来标识。
* */
public class UserDto {
private Integer userId;//用户id
private String userName;//用户名称
private String password;//用户密码
private Integer sex;//用户性别 0女 1男
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
}
3.2 用户服务类
UserService 接口类(路径 src/main/java/com/fishpro/webservice/service/UserService.java)
/**
* 用户服务类 必须使用 @WebService
* */
@WebService(targetNamespace = WsConst.NAMESPACE_URI ,name = "userPortType")
public interface UserService {
/**
* 根据用户id获取用户信息
* */
@WebMethod(operationName="getUserById")
UserDto get(@WebParam(name = "id") String id);
/**
* 获取全部用户信息
* */
@WebMethod(operationName="getUsers")
List<UserDto> list();
/**
* 获取用户数
* */
@WebMethod(operationName="count")
int count(@);
/**
* 新增用户
* */
@WebMethod(operationName="save")
int save(@WebParam(name = "user") UserDto user);
/**
* 更新用户
* */
@WebMethod(operationName="update")
int update(@WebParam(name = "user") UserDto user);
/**
* 删除用户
* */
@WebMethod(operationName="remove")
int remove(@WebParam(name = "id") Integer id);
/**
* 批量删除用户
* */
@WebMethod(operationName="batchRemove")
int batchRemove(@WebParam(name = "ids") Integer[] ids);
}
UserServiceImpl 接口类(路径 src/main/java/com/fishpro/webservice/service/UserServiceImpl.java)
@WebService(
targetNamespace = WsConst.NAMESPACE_URI, //wsdl命名空间
name = "userPortType", //portType名称 客户端生成代码时 为接口名称
serviceName = "userService", //服务name名称
portName = "userPortName", //port名称
endpointInterface = "com.fishpro.webservice.service.UserService")//指定发布webservcie的接口类,此类也需要接入@WebService注解
public class UserServiceImpl implements UserService {
@Override
public UserDto get(String id){
if(null==id){
return null;
}
List<UserDto> list= getData();
UserDto UserDto=null;
for (UserDto user:list
) {
if(id.equals(user.getUserId().toString())){
UserDto=user;
break;
}
}
return UserDto;
}
@Override
public List<UserDto> list(){
List<UserDto> list=new ArrayList<>();
list=getData();
return list;
}
@Override
public int count(){
return getData().size();
}
@Override
public int save(UserDto user){
return 1;
}
@Override
public int update(UserDto user){
return 1;
}
@Override
public int remove(Integer id){
return 1;
}
@Override
public int batchRemove(Integer[] ids){
return 1;
}
/**
* 模拟一组数据
* */
private List<UserDto> getData(){
List<UserDto> list=new ArrayList<>();
UserDto UserDto=new UserDto();
UserDto.setUserId(1);
UserDto.setUserName("admin");
list.add(UserDto);
UserDto=new UserDto();
UserDto.setUserId(2);
UserDto.setUserName("heike");
list.add(UserDto);
UserDto=new UserDto();
UserDto.setUserId(3);
UserDto.setUserName("tom");
list.add(UserDto);
UserDto=new UserDto();
UserDto.setUserId(4);
UserDto.setUserName("mac");
list.add(UserDto);
return list;
}
}
4 服务发布
编写 CxfWebServiceConfig(路径 src/main/java/com/fishpro/webservice/config/CxfWebServiceConfig.java)
import com.fishpro.websevice.service.UserService;
import com.fishpro.websevice.service.impl.UserServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;
@Configuration
public class CxfWebServiceConfig {
/**
*
* */
@Bean("cxfServletRegistration")
public ServletRegistrationBean dispatcherServlet(){
return new ServletRegistrationBean(new CXFServlet(),"/ws/*");
}
/**
* 申明业务处理类 当然也可以直接 在实现类上标注 @Service
*/
@Bean
public UserService userService() {
return new UserServiceImpl();
}
/*
* 非必要项
*/
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
SpringBus springBus = new SpringBus();
return springBus;
}
/*
* 发布endpoint
*/
@Bean
public Endpoint endpoint( ) {
EndpointImpl endpoint = new EndpointImpl(springBus(), userService());
endpoint.publish("/user");//发布地址
return endpoint;
}
}
打开浏览器输入 http://localhost:8080/ws/user?wsdl 可以见到发布的效果
如何使用 Spring Boot 调用 WebService ,请阅读 Spring Boot 使用 CXF 调用 WebService 服务
5 问题
- cxf 的服务方法中,是不能使用java.util.Map作为参数的,因为本身不支持转换
cxf对很多复杂类型支持并不友好,建议参数能使用简单的类型,就使用简单的类型
参考:
https://github.com/apache/cxf
[https://blog.lqdev.cn/2018/11/12/springboot/chapter-thirty-four/]https://blog.lqdev.cn/2018/11/12/springboot/chapter-thirty-four/
Spring Boot 开发 WebService 服务的更多相关文章
- Spring boot 开发WebService遇到的问题之一
当pom.xml文件中的配置: <artifactId>spring-boot-starter-parent</artifactId><version>2.0.6. ...
- Spring Boot开发RESTful接⼝服务及单元测试
Spring Boot开发RESTful接⼝服务及单元测试 常用注解解释说明: @Controller :修饰class,⽤来创建处理http请求的对象 @RestController :Spring ...
- Spring Boot SOAP Webservice例子
前言 本文将学习如何利用Spring boot快速创建SOAP webservice服务: 虽然目前REST和微服务越来越流行,但是SOAP在某些情况下,仍然有它的用武之地: 在本篇 spring b ...
- 一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事
微服务架构 微服务的诞生并非偶然,它是在互联网高速发展,技术日新月异的变化以及传统架构无法适应快速变化等多重因素的推动下诞生的产物.互联网时代的产品通常有两类特点:需求变化快和用户群体庞大,在这种情况 ...
- 一文读懂spring boot 和微服务的关系
欢迎访问网易云社区,了解更多网易技术产品运营经验. Spring Boot 和微服务没关系, Java 微服务治理框架普遍用的是 Spring Cloud. Spring Boot 产生的背景,是开发 ...
- Spring Boot开发HTTPS协议的REST接口
Spring Boot开发HTTP的REST接口流程在前文中已经描述过,见<SpringBoot开发REST接口>. 如需要支持HTTPS,只需要在如上基础上进行设置.修改/resourc ...
- Spring Boot、微服务架构和大数据
一文读懂 Spring Boot.微服务架构和大数据治理三者之间的故事 https://www.cnblogs.com/ityouknow/p/9034377.html 微服务架构 微服务的诞生并非偶 ...
- Spring Boot 开发微信公众号后台
Hello 各位小伙伴,松哥今天要和大家聊一个有意思的话题,就是使用 Spring Boot 开发微信公众号后台. 很多小伙伴可能注意到松哥的个人网站(http://www.javaboy.org)前 ...
- 天天玩微信,Spring Boot 开发私有即时通信系统了解一下
1/ 概述 利用Spring Boot作为基础框架,Spring Security作为安全框架,WebSocket作为通信框架,实现点对点聊天和群聊天. 2/ 所需依赖 Spring Boot 版本 ...
随机推荐
- TFTP服务[精简版]:简单文件传输协议
简单文件传输协议(Trivial File Transfer Protocol,TFTP)是一种基于 UDP 协议在客户端 和服务器之间进行简单文件传输的协议.顾名思义,它提供不复杂.开销不大的文件传 ...
- 【C语言】请输入一个n(n<=10)并输出一个n行n列的杨辉三角
应用二维数组的知识 杨辉三角特点: 1.第一列和对角线的元素全部为1 2.其他元素等于上一行的当前列的值和上一行中当前列前边一列的值之和 #include<stdio.h> #define ...
- js对象冒充实现的继承
//人类 function Person(name) { this.name = name; this.showName = function () { console.log("my na ...
- springboot12(rabbitmq)
RabbitAutoConfiguration @Configuration @ConditionalOnClass({ RabbitTemplate.class, Channel.class }) ...
- 主库增加表空间导致DG同步失败
由于主库表空间不足,同事给表空间增加数据文件,第二天收到反馈说备库未同步. 1.主.备查看归档序列号,发现主.备归档正常同步. SQL>archive log list 2.在主库端查询v$ar ...
- kali 安装与配置
打开虚拟机 新建一个虚拟机 导入虚拟文件 然后进行下面的步骤 开启虚拟机 语言:中文简体 地区: 中国 语言: 汉语 自动安装 配置网络 配置域名 填写密码(两次一致) 自动校对时钟 使用整个磁盘 选 ...
- 1.2、初识WebRTC
文章导读:本节内容,如标题所讲,“初识webrtc”.读完之后,我需要你能清楚三个问题:第一.真正的搞明白实时音视频在生产环境中的真实应用以及前景分析:第二.开发一个符合商业标准的实时音视频应用需要解 ...
- c++中sort函数调用报错Expression : invalid operator <的内部原理
当我们调用sort函数进行排序时,中的比较函数如果写成如下 bool cmp(const int &a, const int &b) { if(a!=b) return a<b; ...
- Linux系统运维工程师入门绝招放送
运维是干嘛的?安装服务器系统?重装系统再装系统?背锅的? 我就稀里糊涂的,这样报着必死的决心,考下RHCE认证,走上了Linux运维的道路,成为了一名linux运维工程师.有些心得跟大家分享下,避免小 ...
- mybatis重新回顾
此次在项目中相遇了mybatis,重新回顾下. 1.resulMap解决了结果集的列名字跟实体setter和getter不匹配的问题 其中property是实体的setter和getter对象,col ...