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

相关推荐
土狗TuGou28 分钟前
SQL内功笔记 · 第8篇:事务的四大特性与隔离级别
数据库·笔记·后端·sql·mysql·oracle
ZengLiangYi37 分钟前
React Query + REST API 最佳实践
javascript·后端·react.js
星浩AI43 分钟前
项目实战:合同智能审批 · LangGraph + HITL 人机协同方案 [有源码]
后端·langchain·agent
JavaGuide44 分钟前
Codex 接入第三方模型 DeepSeek、GLM、Kimi 教程:CC-Switch 和 Codex++ 两种方案对比
后端·ai编程
ZengLiangYi1 小时前
Fastify 加 Electron:把 Web 服务嵌进桌面应用
前端·javascript·后端
在繁华处1 小时前
Java从零到熟练(九):并发编程基础
java·开发语言
木头程序员1 小时前
SSM框架学习笔记
java·开发语言·mysql·spring·maven
李白你好1 小时前
页面资产梳理 · 技术指纹识别 · Spring 端点探测
java·后端·spring
用户1753721240331 小时前
02《面向对象设计原则:SOLID原则实战解析》
后端
一起逃去看海吧1 小时前
dify-03
java·linux·开发语言