SpringBoot整合WebService(实用版)
SpringBoot整合WebService
简介
WebService就是一种跨编程语言和跨操作系统平台的远程调用技术
此处就不赘述WebService相关概念和原理了,可以参考:https://blog.csdn.net/c99463904/article/details/76018436
代码示例
此处共分两个端,客户端和服务端,一个负责调用接口,一个负责创建接口并实现
首先创建一个Maven父项目,在pom.xml文件中引入SpringBoot坐标
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Server搭建
在父项目中创建服务端(Server)子项目,
pom.xml文件,引入以下坐标
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<!-- 不引入会报错 报接口未实现 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.18.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
启动类(引导类)
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class,args);
}
}
User.java
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private Integer age;
}
UserService接口
@WebService(targetNamespace = "wsdl.aerfazhe.com",name = "userPortType")
public interface UserService {
@WebMethod
User getUserName(@WebParam(name = "name") String name);
}
UserService实现类
@WebService(
targetNamespace = "wsdl.aerfazhe.com", //命名空间 指定wsdl说明书在Client端所存放的包路径为com.aerfazhe.wsdl包下
name = "userPortType",
serviceName = "userService", //服务Name名称
portName = "userPortName",
endpointInterface = "com.aerfazhe.service.UserService" // 指定webService的接口类,此类也需要接入@WebService注解
)
public class UserServiceImpl implements UserService {
@Override
public User getUserName(String name) {
User user = new User(name, 28);
return user;
}
}
配置类
@Configuration
public class CxfWebServiceConfig {
@Bean("cxfServletRegistration")
public ServletRegistrationBean<CXFServlet> dispatcherServlet() {
return new ServletRegistrationBean<>(new CXFServlet(),"/ws/*");
}
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
return new SpringBus();
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
@Bean
public Endpoint endpoint(UserService userService) {
EndpointImpl endpoint = new EndpointImpl(springBus(), userService);
endpoint.publish("/user");
return endpoint;
}
}
启动后访问wsdl说明书
http:localhost:8080/ws/user?wsdl

Client搭建
pom.xml
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.2.5</version>
</dependency>
<!-- 不引入会报错 报接口未实现 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.18.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.2.5</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>src/main/resources/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>http://localhost:8080/ws/user?wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
配置类
@Configuration
public class CxfClientConfig {
private final static String SERVICE_ADDRESS = "http://localhost:8080/ws/user";
@Bean("cxfProxy")
public UserPortType createUserPortTypeProxy() {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setAddress(SERVICE_ADDRESS);
jaxWsProxyFactoryBean.setServiceClass(UserPortType.class);
return (UserPortType) jaxWsProxyFactoryBean.create();
}
}
编译转换
将XML格式的wsdl说明书转化为Java对象格式
在Client客户端项目根路径下调出控制台,如下

使用maven命令进行转换,生成的Java代码在Client客户端下的src/main/resources/cxf目录下
mvn generate-sources

查看src/main/resources/cxf下的文件,如下

创建com/aerfazhe/wsdl包路径,将这些JavaObject剪切到该包路径下,如下

Controller
@RestController
public class UserController {
@Resource(name = "cxfProxy")
private UserPortType userPortType;
@GetMapping("/getUserName")
public User getUserName(String name) {
User user = userPortType.getUserName(name);
return user;
}
}
启动类
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class,args);
}
}
application.yml
server:
port: 8081
在当前项目根目录下,输入以下命令,将xml类型的wsdl说明书转化为Java对象
mvn generate-sources

启动后访问:http://localhost:8081/getUserName?name=张三

此时就调用成功喽
SpringBoot整合WebService(实用版)的更多相关文章
- Springboot整合webservice
Springboot整合webservice 2019-12-10 16:34:42 星期二 WebService是什么 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,服务之间的 ...
- springboot整合WebService简单版
一.什么是webservice 关于webservice的介绍摘自百度百科,上面的介绍很详细.(链接:https://baike.baidu.com/item/Web%20Service/121503 ...
- springboot整合webservice采用CXF技术
转载自:https://blog.csdn.net/qq_31451081/article/details/80783220 强推:https://blog.csdn.net/chjskarl/art ...
- 很详细的SpringBoot整合UEditor教程
很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529 版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...
- springboot 整合 CXF 版本异常 java.lang.NoClassDefFoundError:ServletRegistrationBean
在使用SpringBoot 项目整合webservice组件 CXF的时候,在启动时,抛出异常如下,查阅资料初步判断为版本问题.升级到高版本后正常启动. cxf 刚开始使用版本 3.1.7 后更新为 ...
- idea使用springboot的webservice基于cxf
SpringBoot整合CXF实例: 服务端构建 <dependency> <groupId>org.apache.cxf</groupId> <artifa ...
- SpringBoot整合Apache-CXF实践
一.Apache CXF是什么? Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS . ...
- Spring整合CXF步骤,Spring实现webService,spring整合WebService
Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
随机推荐
- 因果推断-Caual Inference
两种形式 Reduced Form:Let data speak itself,主要采用regression等方法 Structure Approach:Data only can never rev ...
- 浅谈php GC(垃圾回收)机制及其与CTF的一点缘分
0x00 侠客日常(一):CTF江湖试剑 众所周知,在php中,当对象被销毁时会自动调用__destruct()方法,同时也要知道,如果程序报错或者抛出异常,则就不会触发该魔术方法. 看题: < ...
- mysql大表修改工具: pt-online-schame-change
在表数据量很大的时候直接添加字段,以及其他表结构修改,会严重影响线上使用,而且耗费时间很长:使用这个工具可以很好的在线修改表结构. 好处: 降低主从延时的风险 可以限速.限资源,避免操作时MySQL负 ...
- linux高级编程之线程间的通信(pthread_cleanup_push和pthread_cleanup_pop)
linux高级编程之线程间的通信(pthread_cleanup_push和pthread_cleanup_pop) 线程可以安排他退出时需要调用的函数,这与进程可以用atexit函数安排进程退出时需 ...
- 机器学习06-(支持向量机SVM、网格搜索、文本分词、词袋模型、词频、文本分类-主题识别)
机器学习-06 机器学习-06 支持向量机(SVM) 支持向量机原理 网格搜索 情感分析 文本分词 词袋模型 词频(TF) 文档频率(DF) 逆文档频率(IDF) 词频-逆文档频率(TF-IDF) 文 ...
- [双目视差] 立体匹配算法推理 - SGBM算法(二)
文章目录 立体匹配算法推理 - SGBM算法(二) 一.SGM算法 二. 后处理 立体匹配算法推理 - SGBM算法(二) 一.SGM算法 SGM算法的全称为Semi-Global Matching, ...
- 百度飞桨(PaddlePaddle)- 张量(Tensor)
飞桨 使用张量(Tensor) 来表示神经网络中传递的数据,Tensor 可以理解为多维数组,类似于 Numpy 数组(ndarray) 的概念.与 Numpy 数组相比,Tensor 除了支持运行在 ...
- DVWA文件上传
Low安全级别: <?php @eval($_POST['cmd']);?> 文本编辑器写入一句话木马,保存并改为php文件. 上传 成功上传,并返回路径 打开菜刀客户端连接 成功连接 反 ...
- 2021-11-27:给定一个数组arr,长度为N,做出一个结构,可以高效的做如下的查询: 1) int querySum(L,R) : 查询arr[L...R]上的累加和; 2) int query
2021-11-27:给定一个数组arr,长度为N,做出一个结构,可以高效的做如下的查询: int querySum(L,R) : 查询arr[L-R]上的累加和; int queryAim(L,R) ...
- Grafana系列-统一展示-9-Jaeger数据源
系列文章 Grafana 系列文章 配置 Jaeger data source Grafana内置了对Jaeger的支持,它提供了开源的端到端分布式跟踪.本文解释了针对Jaeger数据源的配置和查询. ...