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

相关推荐
我登哥MVP9 分钟前
Spring Boot 从“会用”到“精通”:ReturnValueHandler原理
java·spring boot·后端·spring·java-ee·maven·intellij-idea
snow@li12 分钟前
数据库:MySQL vs PostgreSQL 详尽对比(2026版)
java·mysql·postgresql
丑过三八线16 分钟前
Runc 深度解析:从原理到实操
java·linux·开发语言·docker·容器·rpc
伊布拉西莫18 分钟前
Flask 请求生命周期
后端·python·flask
STDD18 分钟前
ntfy 自托管推送通知服务搭建:一条 curl 命令向手机发送通知
java·开发语言·智能手机
英豪16321 分钟前
@Target + @Retention + isAnnotationPresent + getAnnotation
后端
黄同学real25 分钟前
HJL WebAPI 项目日志入库实战:从建表到自动清理
后端
孟陬26 分钟前
国外技术周刊 #140:在 Jeff Bezos 的私密 Campfire 峰会上,我学到了关于亿万富翁的事
前端·后端