SpringBoot 如何调用 WebService 接口

news2025/3/12 15:44:28

前言

调用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://*******/” 给接口提供的域名地址

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

算法 之 树形dp 树的中心、重心

文章目录 重心实践题目小红的陡峭值 在树的算法中&#xff0c;求解树的中心和重心是一类十分重要的算法 求解树的重心 树的重心的定义&#xff1a;重心是树中的一个节点&#xff0c;如果将这个点删除后&#xff0c;剩余各个连通块中点数的最大值最小&#xff0c;那么这个节点…

Docker 配置镜像源

》》Daemon {"registry-mirrors": ["https://docker.1ms.run","https://docker.xuanyuan.me"] }》》》然后在重新 docker systemctl restart docker

Linux 离线部署Ollama和DeepSeek-r1模型

都在复制粘贴联网状态下linux部署deepseek&#xff0c;离线状态下需要下载Ollama和DeepSeek模型&#xff0c;然后将下载包上传到linux中。 1、下载Ollama https://github.com/ollama/ollama/releases 注意&#xff1a;如果CentOS7建议安装V0.5.11版本&#xff0c;V0.5.13需要…

SQLAlchemy系列教程:如何执行原生SQL

Python中的数据库交互提供了高级API。但是&#xff0c;有时您可能需要执行原始SQL以提高效率或利用数据库特定的特性。本指南介绍在SQLAlchemy框架内执行原始SQL。 在SQLAlchemy中执行原生SQL SQLAlchemy虽然以其对象-关系映射&#xff08;ORM&#xff09;功能而闻名&#xff…

RuleOS:区块链开发的“新引擎”,点燃Web3创新之火

RuleOS&#xff1a;区块链开发的“新引擎”&#xff0c;点燃Web3创新之火 在区块链技术的浪潮中&#xff0c;RuleOS宛如一台强劲的“新引擎”&#xff0c;为个人和企业开发去中心化应用&#xff08;DApp&#xff09;注入了前所未有的动力。它以独特的设计理念和强大的功能特性&…

【编译器】VSCODE烧录ESP32-C3——xiaozhi智能聊天机器人固件

【编译器】VSCODE烧录ESP32-C3——xiaozhi智能聊天机器人固件 文章目录 [TOC](文章目录) 前言一、方法一&#xff1a;使用固件烧录工具1. 安装CH340驱动2. 打开FLASH_DOWNLOAD文件3. 选择芯片类型和烧录方式4. 选择烧录文件5. 参数配置 二、方法二&#xff1a;VSCODE导入工程1.…

显式 GC 的使用:留与去,如何选择?

目录 一、什么是显式 GC&#xff1f; &#xff08;一&#xff09; 垃圾回收的基本原理 &#xff08;二&#xff09;显式 GC 方法和行为 1. System.gc() 方法 2. 显式 GC 的行为 &#xff08;三&#xff09;显式 GC 的使用场景与风险 1. JVM 如何处理显式 GC 2. 显式 GC…

SpringMVC概述以及入门案例

目录 SpringMVC概述 为什么需要Spring MVC&#xff1f; SpringMVC入门 工作流程分析 SpringMVC概述 SpringMVC技术与Servlet技术功能等同&#xff0c;均属于Web层开发技术。SpringMVC是一种基于java实现MVC模型的轻量级Web框架。 为什么需要Spring MVC&#xff1f; 在传统J…

⭐LeetCode周赛 3468. 可行数组的数目——暴力与数学⭐

⭐LeetCode周赛 3468. 可行数组的数目——暴力与数学⭐ 示例 1&#xff1a; 输入&#xff1a;original [1,2,3,4], bounds [[1,2],[2,3],[3,4],[4,5]] 输出&#xff1a;2 解释&#xff1a; 可能的数组为&#xff1a; [1, 2, 3, 4] [2, 3, 4, 5] 示例 2&#xff1a; 输入&…

javaEE初阶————多线程进阶(2)

今天来继续带大家学习多线程进阶部分啦&#xff0c;今天是最后一期啦&#xff0c;下期带大家做一些多线程的题&#xff0c;我们就可以开始下一个环节啦&#xff1b; 1&#xff0c;JUC&#xff08;java.util.concurrent&#xff09;的常见类 1&#xff09;Callable 接口 我们之…

maven无法解析插件 org.apache.maven.plugins:maven-jar-plugin:3.4.1

解决流程 1.修改maven仓库库地址 2.删除本地的maven仓库 maven插件一直加载有问题: 无法解析插件 org.apache.maven.plugins:maven-jar-plugin:3.4.1 开始以为maven版本有问题&#xff0c;重装了maven&#xff0c;重装了idea工具。结果问题还是没解决。研究之后发现&#xf…

Android Studio右上角Gradle 的Task展示不全

Android Studio 版本如下&#xff1a;Android Studio lguana|2023.21, 发现Gradle 的Tasks阉割严重&#xff0c;如下图&#xff0c;只显示一个other 解决方法如下&#xff1a;**Setting>Experimental>勾选Configure all gradle tasks during Gradle Sync(this can make…

UDP协议 TCP协议(格式 超时重传 滑动窗口 拥塞控制...)

UDP协议 格式 UDP协议头部格式由8个字节组成&#xff0c;由4个2字节大小的字段组成。 源端口&#xff08;Source Port&#xff0c;16 位&#xff09;&#xff1a; 发送端的端口号&#xff0c;标识数据从哪个端口发出。如果不需要&#xff0c;则可以填 0。 目标端口&#xff0…

爱普生温补晶振 TG5032CFN高精度稳定时钟的典范

在科技日新月异的当下&#xff0c;众多领域对时钟信号的稳定性与精准度提出了极为严苛的要求。爱普生温补晶振TG5032CFN是一款高稳定性温度补偿晶体振荡器&#xff08;TCXO&#xff09;。该器件通过内置温度补偿电路&#xff0c;有效抑制环境温度变化对频率稳定性的影响&#x…

【网络安全工程】任务11:路由器配置与静态路由配置

目录 一、概念 二、路由器配置 三、配置静态路由CSDN 原创主页&#xff1a;不羁https://blog.csdn.net/2303_76492156?typeblog 一、概念 1、路由器的作用&#xff1a;通过路由表进行数据的转发。 2、交换机的作用&#xff1a;通过学习和识别 MAC 地址&#xff0c;依据 M…

Webservice创建

Webservice创建 服务端创建 3层架构 service注解&#xff08;commom模块&#xff09; serviceimpl&#xff08;server&#xff09; 服务端拦截器的编写 客户端拦截器 客户端调用服务端&#xff08;CXF代理&#xff09; 客户端调用服务端&#xff08;动态模式调用&a…

使用VS Code remote ssh进行远程开发的笔记

本文是在VS Code中使用 remote ssh 进行开发的笔记。 安装插件 打开VS Code&#xff0c;在扩展区找到remote相关插件&#xff0c;安装之。下图中红色框出来的是已经安装了的插件&#xff08;圆圈处即为Remote Explorer&#xff09;。 实践 连接服务器 新建连接&#xff1a…

C语言每日一练——day_3(快速上手C语言)

引言 针对初学者&#xff0c;每日练习几个题&#xff0c;快速上手C语言。第三天。&#xff08;会连续更新&#xff09; 采用在线OJ的形式 什么是在线OJ&#xff1f; 在线判题系统&#xff08;英语&#xff1a;Online Judge&#xff0c;缩写OJ&#xff09;是一种在编程竞赛中用…

PostgreSQL - Windows PostgreSQL 下载与安装

Windows PostgreSQL 下载与安装 1、PostgreSQL 下载 下载地址&#xff1a;https://www.enterprisedb.com/downloads/postgres-postgresql-downloads 2、PostgreSQL 安装 启动安装程序 -> 点击 【Next】 指定安装路径 -> 点击 【Next】 默认勾选 -> 点击 【Next】 指…

JVM 的主要组成部分及其作用?

创作内容丰富的干货文章很费心力&#xff0c;感谢点过此文章的读者&#xff0c;点一个关注鼓励一下作者&#xff0c;激励他分享更多的精彩好文&#xff0c;谢谢大家&#xff01; JVM包含两个子系统和两个组件&#xff0c;两个子系统为Class loader(类装载)、Execution engine(执…