当前位置: 首页 > news >正文

spring boot 使用 webservice

spring boot 使用 webservice

使用 java 自带的 jax-ws

依赖

如果是jdk1.8,不需要引入任何依赖,如果大于1.8

<dependency><groupId>javax.jws</groupId><artifactId>javax.jws-api</artifactId><version>1.1</version>
</dependency>
<dependency><groupId>javax.xml.ws</groupId><artifactId>jaxws-api</artifactId>
</dependency>

定义服务接口

使用 @WebService 注解

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HumanService {@WebMethodpublic boolean addHuman(Human human);@WebMethodpublic boolean deleteHuman(String name);@WebMethodpublic Human get(String name);/*** 不能处理List, 只能处理数组* @return*/@WebMethodpublic Human[] getAll();}

实现

package com.example.demoweb.webservice.impl;import com.example.demoweb.model.Human;
import com.example.demoweb.webservice.HumanService;
import org.springframework.util.CollectionUtils;import javax.jws.WebService;
import java.util.*;/*** @Author: xiaodong.zheng* @Date: 2024/3/7 15:36*/
@WebService(endpointInterface = "com.example.demoweb.webservice.HumanService",serviceName = "HumanService",targetNamespace = "human.ns" //随便写,不过在客户端调用时会用到
)
public class HumanServiceImpl implements HumanService {private Map<String, Human> data = new HashMap<>();@Overridepublic boolean addHuman(Human human) {data.put(human.getName(), human);return true;}@Overridepublic boolean deleteHuman(String name) {data.remove(name);return true;}@Overridepublic Human get(String name) {return data.get(name);}@Overridepublic Human[] getAll() {if (CollectionUtils.isEmpty(data)) {return null;}Human[] hs = new Human[data.size()];int i = 0;for (Map.Entry<String, Human> entry : data.entrySet()) {hs[i] = entry.getValue();i++;}return hs;}
}

暴露端口

@SpringBootApplication
public class DemoWebApplication {public static final String WS_HUMAN = "http://localhost:8888/ws/hh";public static void main(String[] args) {//注意webservice服务发布会优先于spring容器启动,不然 使用依赖注入会失败!!Endpoint.publish(WS_HUMAN, new HumanServiceImpl());SpringApplication.run(DemoWebApplication.class, args);}}

访问 http://localhost:8888/ws/hh?wsdl

在这里插入图片描述

webservice客户端调用

动态调用

@RestController
public class HumanWebServiceClientController {@GetMapping("/index")public String invoke() throws MalformedURLException {//public static final String WS_HUMAN = "http://localhost:8888/ws/hh";URL wsdlURL = new URL(WS_HUMAN + "?wsdl");//默认localPart=实现类+ServiceQName qname = new QName("human.ns", "HumanService");Service service = Service.create(wsdlURL, qname);HumanService humanService = service.getPort(HumanService.class);Human human = new Human();human.setName("tom");human.setAge(12);boolean b = humanService.addHuman(human);System.out.println("add human: " + b);Human[] all = humanService.getAll();System.out.println("get all data: " + JSON.toJSONString(all));return "success";}
}

依赖注入调用

使用 @WebServiceClient 注解, 调用者客户端:

@Component
@WebServiceClient(targetNamespace = "human.ns", wsdlLocation = WS_HUMAN + "?wsdl",name = "HumanService")
public class HumanServiceClient extends Service implements HumanService {public final static QName SERVICE = new QName("human.ns", "HumanService");public HumanServiceClient() throws MalformedURLException {super(new URL(WS_HUMAN + "?wsdl"), SERVICE);}@Overridepublic boolean addHuman(Human human) {return super.getPort(HumanService.class).addHuman(human);}@Overridepublic boolean deleteHuman(String name) {return super.getPort(HumanService.class).deleteHuman(name);}@Overridepublic Human get(String name) {return super.getPort(HumanService.class).get(name);}@Overridepublic Human[] getAll() {return super.getPort(HumanService.class).getAll();}
}

调用

@RestController
public class HumanWebServiceClientController {@Autowiredprivate HumanServiceClient humanServiceClient;@GetMapping("/index2")public String invoke2() throws MalformedURLException {Human human = new Human();human.setName("tom");human.setAge(13);boolean b = humanServiceClient.addHuman(human);System.out.println("add human: " + b);Human[] all = humanServiceClient.getAll();System.out.println("get all data: " + JSON.toJSONString(all));return "success";}}

结果:

add human: true
get all data: [{"age":13,"name":"tom"}]

good luck!

相关文章:

spring boot 使用 webservice

spring boot 使用 webservice 使用 java 自带的 jax-ws 依赖 如果是jdk1.8,不需要引入任何依赖&#xff0c;如果大于1.8 <dependency><groupId>javax.jws</groupId><artifactId>javax.jws-api</artifactId><version>1.1</version&g…...

【嵌入式】嵌入式系统稳定性建设:最后的防线

&#x1f9d1; 作者简介&#xff1a;阿里巴巴嵌入式技术专家&#xff0c;深耕嵌入式人工智能领域&#xff0c;具备多年的嵌入式硬件产品研发管理经验。 &#x1f4d2; 博客介绍&#xff1a;分享嵌入式开发领域的相关知识、经验、思考和感悟。提供嵌入式方向的学习指导、简历面…...

【算法】一类支持向量机OC-SVM

【算法】一类支持向量机OC-SVM 前言一类支持向量机OC-SVM 概念介绍示例编写数据集创建实现一类支持向量机OC-SVM完整的示例输出 前言 由于之前毕设期间主要的工具就是支持向量机&#xff0c;从基础的回归和分类到后来的优化&#xff0c;在接触到支持向量机还有一类支持向量机的…...

深入学习默认成员函数——c++指南

前言&#xff1a;类和对象是面向对象语言的重要概念。 c身为一门既面向过程&#xff0c;又面向对象的语言。 想要学习c&#xff0c; 首先同样要先了解类和对象。 本节就类和对象的几种构造函数相关内容进行深入的解析。 目录 类和对象的基本概念 封装 类域和类体 访问限定符…...

psutil, 一个超级有用的Python库

Python的psutil是一个跨平台的库&#xff0c;可以用于获取系统运行时的各种信息&#xff0c;包括CPU使用率、内存使用情况、磁盘和网络信息等。它主要用来做系统监控&#xff0c;性能分析&#xff0c;进程管理。它实现了同等命令行工具提供的功能&#xff0c;如ps、top、lsof、…...

[Python]`threading.local`创建线程本地数据

在Python中&#xff0c;threading.local是一个用于创建线程本地数据的工具。它允许每个线程拥有自己独立的变量副本&#xff0c;这样可以在多线程程序中避免共享变量带来的问题。 通过使用threading.local&#xff0c;你可以为每个线程创建一个独立的变量空间&#xff0c;这样…...

删除数据表

oracle从入门到总裁:​​​​​​https://blog.csdn.net/weixin_67859959/article/details/135209645 删除数据表属于数据库对象的操作 drop table 表名称; 删除 emp30 表 SQL> drop table emp30;表已删除。 上面这个语句运行后&#xff0c;就会把数据表 emp30 删除 在…...

前端自带的base64转化方法

前端html的base64使用方法window.btoa()和window.atob()_html用window.btoa();-CSDN博客...

图论(二)之最短路问题

最短路 Dijkstra求最短路 文章目录 最短路Dijkstra求最短路栗题思想题目代码代码如下bellman-ford算法分析只能用bellman-ford来解决的题型题目完整代码 spfa求最短路spfa 算法思路明确一下松弛的概念。spfa算法文字说明&#xff1a;spfa 图解&#xff1a; 题目完整代码总结ti…...

.NET Core 日志记录功能详解

在软件开发和运维过程中&#xff0c;日志记录是一个非常重要的功能。它可以帮助开发者跟踪应用程序的运行状况、诊断和监控问题。.NET Core 提供了一个灵活且易于使用的日志系统&#xff0c;本文将详细介绍.NET Core日志的相关概念、配置和使用方法。 1. 什么是日志记录以及它…...

docker——启动各种服务

1.Mysql 2.Redis 3.nginx 4.ES 注意&#xff1a;ES7之后环境为 -e ELASTICSEARCH_HOSTS http://ip地址:9200...

git远程仓库使用

赋值这个地址clone 克隆之后 cd slam_oncloud/ git remote add chenxnew ssh://git192.168.3.40:1022/chenxiao/slam_oncloud.git 查看一下 linuxchenxiao:/media/linux/mydisk/cloud_slam/slam_oncloud$ git remote add chenxnew ssh://git192.168.3.40:1022/chenxiao/sla…...

js导出的excel文件无法打开/打开乱码,excel无法打开xxx.xlsx因为文件格式或文件扩展无效

excel无法打开xxx.xlsx因为文件格式或文件扩展无效 使用 a 标签导出这里就不细说了&#xff0c;直接说上述问题解决方案 在调用导出接口的时候加上两个参数 responseType: “blob” responseEncoding: “utf8” export function test(data) {return util({url: /test,method: …...

透明多级分流系统(用户端缓存和负载均衡)

部件考虑 有些设备位于客户端或者网络边缘&#xff0c;能够迅速响应用户请求&#xff0c;避免给cpu和数据库带来压力&#xff0c;比如&#xff0c;本地缓存&#xff0c;内容分发网络&#xff0c;反向代理等。 有些设备处理能力能够线性扩展&#xff0c;易于伸缩&#xff0c;应…...

Python sort从大到小排序面试题

在Python中&#xff0c;你可以使用内置的sorted()函数或者列表的sort()方法来对列表中的元素进行从大到小的排序。 使用sorted()函数&#xff1a; numbers [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] sorted_numbers sorted(numbers, reverseTrue) # 设置reverseTrue实现从大到小排…...

【Stable Diffusion】入门:AI绘画提示词+参数设置攻略

目录 1 提示词1.1 分类和书写方式1.1.1 内容型提示词1.1.2 标准化提示词1.1.3 通用模板 1.2 权重1.2.1 套括号1.2.2 数字权重1.2.3 进阶语法 1.3 负面提示词 2 参数详解2.1 Sampling steps2.2 Sampling method2.3 Width, Height2.4 CFG Scale2.5 Seed2.6 Batch count, Batch si…...

Qt使用Q_DECLARE_INTERFACE Q_INTERFACES宏实现接口类使用qobject_cast进行类型转换

在写抽象类或者接口的时候&#xff0c;肯定是不能继承QObject的 但是又想使用qobject_cast进行类型转换&#xff0c;使用以下办法就能实现 #ifndef FACTORYINTERFACE_H__ #define FACTORYINTERFACE_H__ #include <QObject> class FactoryInterface{ public:FactoryInterf…...

docker离线搭建仓库

要在Docker中搭建本地仓库&#xff0c;可以按照以下步骤进行操作&#xff1a; 首先安装 Docker。根据不同的操作系统选择合适的版本并完成安装过程。打开命令行工具&#xff08;如Terminal或PowerShell&#xff09;&#xff0c;运行以下命令来创建一个新的容器并将其设置为本地…...

ABC344 A-E题解

文章目录 A题目AC Code&#xff1a; B题目AC Code&#xff1a; C题目AC Code&#xff1a; D题目AC Code&#xff1a; E题目AC Code&#xff1a; 不易不难&#xff0c;写到5题很简单&#xff0c;但是要有足够的思维能力。 A 题目 我们用一个 flag 变量记录我们是不是在两个竖…...

Jeecg-boot 初次启动项目失败

1.将IDEA的字符编码全部改成utf-8 2. 更改database的密码 3.换个jdk重新启动...

RocketMQ延迟消息机制

两种延迟消息 RocketMQ中提供了两种延迟消息机制 指定固定的延迟级别 通过在Message中设定一个MessageDelayLevel参数&#xff0c;对应18个预设的延迟级别指定时间点的延迟级别 通过在Message中设定一个DeliverTimeMS指定一个Long类型表示的具体时间点。到了时间点后&#xf…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接&#xff1a;3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯&#xff0c;要想要能够将所有的电脑解锁&#x…...

基于数字孪生的水厂可视化平台建设:架构与实践

分享大纲&#xff1a; 1、数字孪生水厂可视化平台建设背景 2、数字孪生水厂可视化平台建设架构 3、数字孪生水厂可视化平台建设成效 近几年&#xff0c;数字孪生水厂的建设开展的如火如荼。作为提升水厂管理效率、优化资源的调度手段&#xff0c;基于数字孪生的水厂可视化平台的…...

基于Docker Compose部署Java微服务项目

一. 创建根项目 根项目&#xff08;父项目&#xff09;主要用于依赖管理 一些需要注意的点&#xff1a; 打包方式需要为 pom<modules>里需要注册子模块不要引入maven的打包插件&#xff0c;否则打包时会出问题 <?xml version"1.0" encoding"UTF-8…...

自然语言处理——循环神经网络

自然语言处理——循环神经网络 循环神经网络应用到基于机器学习的自然语言处理任务序列到类别同步的序列到序列模式异步的序列到序列模式 参数学习和长程依赖问题基于门控的循环神经网络门控循环单元&#xff08;GRU&#xff09;长短期记忆神经网络&#xff08;LSTM&#xff09…...

如何在网页里填写 PDF 表格?

有时候&#xff0c;你可能希望用户能在你的网站上填写 PDF 表单。然而&#xff0c;这件事并不简单&#xff0c;因为 PDF 并不是一种原生的网页格式。虽然浏览器可以显示 PDF 文件&#xff0c;但原生并不支持编辑或填写它们。更糟的是&#xff0c;如果你想收集表单数据&#xff…...

Yolov8 目标检测蒸馏学习记录

yolov8系列模型蒸馏基本流程&#xff0c;代码下载&#xff1a;这里本人提交了一个demo:djdll/Yolov8_Distillation: Yolov8轻量化_蒸馏代码实现 在轻量化模型设计中&#xff0c;**知识蒸馏&#xff08;Knowledge Distillation&#xff09;**被广泛应用&#xff0c;作为提升模型…...

三分算法与DeepSeek辅助证明是单峰函数

前置 单峰函数有唯一的最大值&#xff0c;最大值左侧的数值严格单调递增&#xff0c;最大值右侧的数值严格单调递减。 单谷函数有唯一的最小值&#xff0c;最小值左侧的数值严格单调递减&#xff0c;最小值右侧的数值严格单调递增。 三分的本质 三分和二分一样都是通过不断缩…...

LRU 缓存机制详解与实现(Java版) + 力扣解决

&#x1f4cc; LRU 缓存机制详解与实现&#xff08;Java版&#xff09; 一、&#x1f4d6; 问题背景 在日常开发中&#xff0c;我们经常会使用 缓存&#xff08;Cache&#xff09; 来提升性能。但由于内存有限&#xff0c;缓存不可能无限增长&#xff0c;于是需要策略决定&am…...

基于PHP的连锁酒店管理系统

有需要请加文章底部Q哦 可远程调试 基于PHP的连锁酒店管理系统 一 介绍 连锁酒店管理系统基于原生PHP开发&#xff0c;数据库mysql&#xff0c;前端bootstrap。系统角色分为用户和管理员。 技术栈 phpmysqlbootstrapphpstudyvscode 二 功能 用户 1 注册/登录/注销 2 个人中…...