SpringBoot 如何调用 WebService 接口

前言

调用WebService接口的方式有很多,今天记录一下,使用 Spring Web Services 调用 SOAP WebService接口

一.导入依赖

        <!-- Spring Boot Web依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Web Services -->
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>

        <!-- Apache HttpClient 作为 WebService 客户端 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

        <!-- JAXB API -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        <!-- JAXB 运行时 -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.5</version>
        </dependency>

二.创建请求类和响应类

根据SOAP的示例,创建请求类和响应类

SOAP示例

请求
POST *****************
Host: **************
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://*******/DownloadFileByMaterialCode"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <DownloadFileByMaterialCode xmlns="http://*******/">
      <MaterialCode>string</MaterialCode>
      <FileType>string</FileType>
      <Category>string</Category>
    </DownloadFileByMaterialCode>
  </soap:Body>
</soap:Envelope>


响应
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <DownloadFileByMaterialCodeResponse xmlns="http://********/">
      <DownloadFileByMaterialCodeResult>string</DownloadFileByMaterialCodeResult>
    </DownloadFileByMaterialCodeResponse>
  </soap:Body>
</soap:Envelope>

根据我的这个示例,我创建的请求类和响应类,是这样的

请求类

@Data
@XmlRootElement(name = "DownloadFileByMaterialCode", namespace = "http://*******/")
@XmlAccessorType(XmlAccessType.FIELD)
public class DownloadFileByMaterialCodeRequest {
    @XmlElement(name = "MaterialCode", namespace = "http://*******/")
    private String MaterialCode;
    @XmlElement(name = "FileType", namespace = "http://*******/")
    private String FileType;
    @XmlElement(name = "Category", namespace = "http://*******/")
    private String Category;
}

响应类

@Data
@XmlRootElement(name = "DownloadFileByMaterialCodeResponse", namespace = "http://********/")
@XmlAccessorType(XmlAccessType.FIELD)
public class DownloadFileByMaterialCodeResponse {
    @XmlElement(name = "DownloadFileByMaterialCodeResult", namespace = "http://********/")
    private String DownloadFileByMaterialCodeResult;
}

三.创建ObjectFactory类

@XmlRegistry
public class ObjectFactory {
    // 创建 DownloadFileByMaterialCodeRequest 的实例
    public DownloadFileByMaterialCodeRequest createDownloadFileByMaterialCodeRequest() {
        return new DownloadFileByMaterialCodeRequest();
    }

    // 创建 DownloadFileByMaterialCodeResponse 的实例
    public DownloadFileByMaterialCodeResponse createDownloadFileByMaterialCodeResponse() {
        return new DownloadFileByMaterialCodeResponse();
    }
}

四.配置WebServiceTemplate

@Configuration
public class WebServiceConfig {

    @Bean
    public WebServiceTemplate webServiceTemplate() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("org.jeecg.modules.webservice");  // 包路径,包含请求和响应对象类

        WebServiceTemplate template = new WebServiceTemplate(marshaller);

        return template;
    }
}

五.调用WebService接口

@Service
public class DownloadFileService {
    @Autowired
    private WebServiceTemplate webServiceTemplate;

    public List<ResponseJsonObject> downloadFile(String materialCode, String fileType, String category) throws JsonProcessingException {
        String uri = "http://192.168.***.***/DYDServiceTest/PlmService.asmx";  // WebService 的 URL

        // 创建请求对象并设置参数
        DownloadFileByMaterialCodeRequest request = new DownloadFileByMaterialCodeRequest();
        request.setMaterialCode(materialCode);
        request.setFileType(fileType);
        request.setCategory(category);

        // 设置 SOAPAction
        String soapAction = "http://********/DownloadFileByMaterialCode";  // Web 服务指定的 SOAPAction

        // 使用 SoapActionCallback 来设置 SOAPAction 头
        SoapActionCallback soapActionCallback = new SoapActionCallback(soapAction);

        // 发送 SOAP 请求并获取响应
        DownloadFileByMaterialCodeResponse response = (DownloadFileByMaterialCodeResponse)
                webServiceTemplate.marshalSendAndReceive(uri, request, soapActionCallback);

        // 获取并返回 DownloadFileByMaterialCodeResult
        String downloadFileByMaterialCodeResult = response.getDownloadFileByMaterialCodeResult();
        System.out.println(downloadFileByMaterialCodeResult);

        //字符串转换为ResponseJsonObject对象
        ObjectMapper objectMapper = new ObjectMapper();
        // 忽略未知字段
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        List<ResponseJsonObject> ResponseJsonObjects = objectMapper.readValue(downloadFileByMaterialCodeResult, objectMapper.getTypeFactory().constructCollectionType(List.class, ResponseJsonObject.class));

        return ResponseJsonObjects;
    }
}

六.测试代码

    @Test
    public void test1() throws JsonProcessingException {
        List<ResponseJsonObject> responseJsonObjects = downloadFileService.downloadFile("CCPT0016-QBY-7", "", "");
        for (ResponseJsonObject responseJsonObject : responseJsonObjects) {
            System.out.println(responseJsonObject.getDocName());
        }
    }

测试效果

这里在附上所有文件的路劲图,可以参考一下

总结

根据接口给出的SAOP的示例,封装好对应的实体类,因为我这里的类型都是String,大家也可以根据实际情况,封装好对应的类

注意注解的参数,namespace = “http://*******/” 给接口提供的域名地址

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/984967.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

从Manus到OpenManus:多智能体协作框架如何重构AI生产力?

文章目录 Manus&#xff1a;封闭生态下的通用AI智能体OpenManus&#xff1a;开源社区的闪速复刻挑战与未来&#xff1a;框架落地的现实边界当前局限性未来演进方向 OpenManus使用指南1. 环境配置2. 参数配置3. 替换搜索引擎4. 运行效果 协作框架开启AI生产力革命 Manus&#xf…

1.5 双指针专题:有效三⻆形的个数(medium)

1.题目链接 611. 有效三角形的个数 - 力扣&#xff08;LeetCode&#xff09;https://leetcode.cn/problems/valid-triangle-number/submissions/609232447/ 2.题目描述 给定⼀个包含⾮负整数的数组 nums &#xff0c;返回其中可以组成三⻆形三条边的三元组个数。 ⽰例 1: 输…

大数据学习(59)-DataX执行机制

&&大数据学习&& &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 承认自己的无知&#xff0c;乃是开启智慧的大门 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4dd;支持一下博主哦&#x1f91…

《面向对象程序设计-C++》实验一 熟悉Visual C++开发环境及上机过程

一、实验目的 了解和使用VC集成开发环境&#xff1b;熟悉VC环境的基本命令和功能键&#xff1b;熟悉常用的功能菜单命令&#xff1b;学习使用VC环境的帮助&#xff1b;学习完整的C程序开发过程&#xff1b;理解简单的C程序结构。 二、实验内容 使用Visual C 6.0集成环境来编…

Chebykan wx 文章阅读

文献筛选 [1] 神经网络&#xff1a;全面基础 [2] 通过sigmoid函数的超层叠近似 [3] 多层前馈网络是通用近似器 [5] 注意力是你所需要的 [6] 深度残差学习用于图像识别 [7] 视觉化神经网络的损失景观 [8] 牙齿模具点云补全通过数据增强和混合RL-GAN [9] 强化学习&#xff1a;一…

2025解决软件供应链安全,开源安全的版本答案:SCA+SBOM

GitHub&#xff1a; https://github.com/XmirrorSecurity/OpenSCA-cli/ Gitee&#xff1a; https://gitee.com/XmirrorSecurity/OpenSCA-cli/ OpenSCA官网&#xff1a; https://opensca.xmirror.cn/ 根据Sonatype 发布的《软件供应链现状》报告&#xff0c;其中强调软件供…

Linux 系统负载过高的排查思路

技术探讨&#xff1a;Linux系统负载过高的排查思路 在Linux服务器运行过程中&#xff0c;如果系统负载过高&#xff0c;可能会导致性能下降和服务不稳定。以下是针对Linux系统负载过高问题的排查思路和解决方法&#xff1a; 1. 查看系统负载&#xff1a; 使用uptime或top命令查…

typora高亮方案+鼠标侧键一键改色

引言 在typora里面有一个自定义的高亮, <mark></mark>>但是单一颜色就太难看了, 我使用人工智能, 搜索全网艺术家, 汇集了几种好看的格式,并且方便大家侧键一键 调用, 是不是太方便啦 ! 示例 午夜模式 春意盎然 深海蓝调 石墨文档 秋日暖阳 蜜桃宣言 使用方法 …

自然语言处理文本分析:从词袋模型到认知智能的进化之旅

清晨&#xff0c;当智能音箱准确识别出"播放周杰伦最新专辑"的模糊语音指令时&#xff1b;午间&#xff0c;企业舆情系统自动标记出十万条评论中的负面情绪&#xff1b;深夜&#xff0c;科研人员用GPT-4解析百万篇论文发现新材料线索——这些场景背后&#xff0c;是自…

基于SSM+Vue+uniapp的考研交流(带商城)小程序+LW示例参考

系列文章目录 1.基于SSM的洗衣房管理系统原生微信小程序LW参考示例 2.基于SpringBoot的宠物摄影网站管理系统LW参考示例 3.基于SpringBootVue的企业人事管理系统LW参考示例 4.基于SSM的高校实验室管理系统LW参考示例 5.基于SpringBoot的二手数码回收系统原生微信小程序LW参考示…

浙江大学:DeepSeek行业应用案例集(153页)(文末可下载PDF)

浙江大学&#xff1a;DeepSeek行业应用案例集&#xff08;153页&#xff09;&#xff08;文末可下载PDF&#xff09; 全文链接&#xff1a;浙江大学&#xff1a;DeepSeek行业应用案例集&#xff08;153页&#xff09;&#xff08;文末可下载PDF&#xff09; | AI探金 全文链接&…

深度学习分类回归(衣帽数据集)

一、步骤 1 加载数据集fashion_minst 2 搭建class NeuralNetwork模型 3 设置损失函数&#xff0c;优化器 4 编写评估函数 5 编写训练函数 6 开始训练 7 绘制损失&#xff0c;准确率曲线 二、代码 导包&#xff0c;打印版本号&#xff1a; import matplotlib as mpl im…

共享经济时代下,鲲鹏共享科技如何逆袭改命?

2016年&#xff0c;当共享充电宝顶着“资本泡沫”的质疑横空出世时&#xff0c;没人能想到&#xff0c;这个曾被王思聪嘲讽“能成我吃翔”的行业&#xff0c;竟在短短几年内成为共享经济领域最顽强的幸存者。数据显示&#xff0c;2019年共享充电宝用户规模突破3亿&#xff0c;单…

说一下spring的事务隔离级别?

大家好&#xff0c;我是锋哥。今天分享关于【说一下spring的事务隔离级别&#xff1f;】面试题。希望对大家有帮助&#xff1b; 说一下spring的事务隔离级别&#xff1f; 1000道 互联网大厂Java工程师 精选面试题-Java资源分享网 Spring的事务隔离级别是指在数据库事务管理中…

Web开发第五节

一.结构伪类选择器 &#xff08;一&#xff09;选择单个 &#xff08;二&#xff09;选择多个 注&#xff1a;1.n5指的是5以后的数字&#xff0c;包含5&#xff0c;n从0开始 2.-n5指的是5以前的数字&#xff0c;同样包含5&#xff0c;并且n从0开始 二.伪元素选择器 注&…

计算机毕业设计:驾校综合信息系统

驾校综合信息系统mysql数据库创建语句驾校综合信息系统oracle数据库创建语句驾校综合信息系统sqlserver数据库创建语句驾校综合信息系统springspringMVChibernate框架对象(javaBean,pojo)设计驾校综合信息系统springspringMVCmybatis框架对象(javaBean,pojo)设计 驾校综合信息系…

无标签数据增强+高效注意力GAN:基于CARLA的夜间车辆检测精度跃升

目录 一、摘要 二、引言 三、框架 四、方法 生成合成夜间数据 昼夜图像风格转换 针对夜间图像的无标签数据增强技术 五、Coovally AI模型训练与应用平台 六、实验 数据 图像风格转换 夜间车辆检测和分类 结论 论文题目&#xff1a;ENHANCING NIGHTTIME VEHICLE D…

RocketMQ面试题:原理部分

&#x1f9d1; 博主简介&#xff1a;CSDN博客专家&#xff0c;历代文学网&#xff08;PC端可以访问&#xff1a;https://literature.sinhy.com/#/?__c1000&#xff0c;移动端可微信小程序搜索“历代文学”&#xff09;总架构师&#xff0c;15年工作经验&#xff0c;精通Java编…

NAFNet:Simple Baselines for Image Restoration

Abstract 近年来&#xff0c;图像复原技术取得了长足的进步&#xff0c;但现有的图像复原方法&#xff08;SOTA&#xff09;系统复杂度也在不断增加&#xff0c;不利于对各种方法的分析和比较。在本文中&#xff0c;我们提出了一种简单的基线&#xff0c;它超越了SOTA方法&…

FlinkCDC3.3 使用 Mysql 8.4 报错

一、报错日志 Caused by: io.debezium.DebeziumException: org.apache.flink.util.FlinkRuntimeException: Cannot read the binlog filename and position via SHOW MASTER STATUS. Make sure your server is correctly configuredat org.apache.flink.cdc.connectors.mysql.…