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(实用版)的更多相关文章

  1. Springboot整合webservice

    Springboot整合webservice 2019-12-10 16:34:42 星期二 WebService是什么 WebService是一种跨编程语言和跨操作系统平台的远程调用技术,服务之间的 ...

  2. springboot整合WebService简单版

    一.什么是webservice 关于webservice的介绍摘自百度百科,上面的介绍很详细.(链接:https://baike.baidu.com/item/Web%20Service/121503 ...

  3. springboot整合webservice采用CXF技术

    转载自:https://blog.csdn.net/qq_31451081/article/details/80783220 强推:https://blog.csdn.net/chjskarl/art ...

  4. 很详细的SpringBoot整合UEditor教程

    很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529    版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...

  5. springboot 整合 CXF 版本异常 java.lang.NoClassDefFoundError:ServletRegistrationBean

    在使用SpringBoot 项目整合webservice组件 CXF的时候,在启动时,抛出异常如下,查阅资料初步判断为版本问题.升级到高版本后正常启动. cxf 刚开始使用版本  3.1.7 后更新为 ...

  6. idea使用springboot的webservice基于cxf

    SpringBoot整合CXF实例: 服务端构建 <dependency> <groupId>org.apache.cxf</groupId> <artifa ...

  7. SpringBoot整合Apache-CXF实践

    一.Apache CXF是什么? Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS . ...

  8. Spring整合CXF步骤,Spring实现webService,spring整合WebService

    Spring整合CXF步骤 Spring实现webService, spring整合WebService >>>>>>>>>>>> ...

  9. spring-boot整合mybatis(1)

    sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...

  10. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

随机推荐

  1. windows系统git使用ssh方式和gitee/github进行同步

    前言 在从github/gitee远程仓库获取代码时,除了使用https方式,我们还可以使用ssh连接的方式与远程仓库服务器通信,其好处是有时会比https更方便.稳定.快速. 和与普通的linux服 ...

  2. 使用js截取路径参数方法

    1.根据传入的路径和参数名称截取 export function getUrlParams(href,name) { var reg = new RegExp("(^|\\?|&)& ...

  3. Cesium加载ArcGIS Server4490且orgin -400 400的切片服务

    Cesium在使用加载Cesium.ArcGisMapServerImageryProvider加载切片服务时,默认只支持wgs84的4326坐标系,不支持CGCS2000的4490坐标系. 如果是A ...

  4. 笔记九:线程间的通信(pthread_create()和pthread_self())

    linux高级编程之线程间的通信:(pthread_create().pthread_self()) 1.线程概念        线程包含了表示进程内执行环境必须得信息,其中包括进程中标识线程的线程I ...

  5. 数据结构(DataStructure)-01

    数据结构-01 **数据结构与算法** **算法概述** **时间复杂度概述** **时间复杂度 - 计算规则** **数据结构概述** **抽象数据类型** **线性表 - 顺序表** **线性表 ...

  6. 从案例中详解go-errgroup-源码

    一.背景 某次会议上发表了error group包,一个g失败,其他的g会同时失败的错误言论(看了一下源码中的一句话The first call to return a non-nil error c ...

  7. 2022-10-05:在一个 n x n 的整数矩阵 grid 中, 每一个方格的值 grid[i][j] 表示位置 (i, j) 的平台高度。 当开始下雨时,在时间为 t 时,水池中的水位为 t 。

    2022-10-05:在一个 n x n 的整数矩阵 grid 中, 每一个方格的值 grid[i][j] 表示位置 (i, j) 的平台高度. 当开始下雨时,在时间为 t 时,水池中的水位为 t . ...

  8. Cobalt Strike 连接启动教程(1)

      第一步:把cobaltstrike4(解压后)拷贝到虚拟机Kali系统的root目录下 第二步:进入cobalstrike4文件夹中 第三步:选寻kali系统 IP地址 第四步: 启动服务端:(t ...

  9. 【QCustomPlot】下载

    说明 使用 QCustomPlot 绘图库辅助开发时整理的学习笔记.同系列文章目录可见 <绘图库 QCustomPlot 学习笔记>目录.本篇介绍 QCustomPlot 的下载. 目录 ...

  10. 判断两个矩形是否相交(Rect Intersection)

    0x00 Preface 最近在开发一个2D组态图形组件的过程中,里面的数学模块,涉及到两个矩形是否相交的判断. 这个问题很多年前就写过,算是个小的算法吧. 网络上搜索一下,有很多思路,有一些思路要基 ...