美文网首页
Retrofit2+Okhttp3+Rxjava通过SOAP协议

Retrofit2+Okhttp3+Rxjava通过SOAP协议

作者: 风一样的存在 | 来源:发表于2024-07-29 10:24 被阅读0次
1. 请求异步与同步问题
2. 请求和响应的序列化和反序列化问题

由于在web端soap协议交互,会遇到异步和同步的问题,代码参考如下:

Call<ResponseBody> call = apiService.getRatePlan(envelope);
//异步请求
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call,Response<ResponseBody> response) {
          log.info("onResponse: {}", response.body().toString());
    }
    @Override
    public void onFailure(Call<ResponseBody> call, Throwable throwable) {
          log.info("onFailure: {}", throwable.getMessage());
    }
});

如果想要同步可以使用下面的语法:

try {
    Response<ResponseBody> response = call.execute();
    if(response.isSuccessful()) {
        String result = response.body().string();
        log.info("result:{}",result);
    }
} catch (Exception e) {
    log.error(e.getMessage());
}

对象序列化xml成功,但是xml反序列化对象就一直为空,以下是测试的一段报文

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <RatePlanAllotRS xmlns="http://gv.knt.co.jp/">
            <AllotReposeSegments>
                <AllotReposeSegment Number="1">
                    <RatePlan RatePlanCode="000403002610104017" HotelCode="2610104">
                        <SearchDateRange Start="2020-01-01" End="2020-01-31" />
                        <AllotmentRange>
                            <Allotment>
                                <Text>3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3</Text>
                            </Allotment>
                        </AllotmentRange>
                    </RatePlan>
                </AllotReposeSegment>
            </AllotReposeSegments>
        </RatePlanAllotRS>
    </soap:Body>
</soap:Envelope>

实体类信息

import lombok.Data;
import org.simpleframework.xml.*;

import java.util.List;

@Data
@Root(name = "soap:Envelope",strict = false)
@NamespaceList({
        @Namespace(reference = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi"),
        @Namespace(reference = "http://www.w3.org/2001/XMLSchema", prefix = "xsd"),
        @Namespace(reference = "http://schemas.xmlsoap.org/soap/envelope/", prefix = "soap")
})
public class RatePlanAllotRespEnvelope {
    @Element(name = "soap:Body",required = false)
    private RatePlanAllotBody body;

    @Data
    public static class RatePlanAllotBody {
        @Element(name = "RatePlanAllotRS",required = false)
        @Namespace(reference = "http://gv.knt.co.jp/")
        private RatePlanAllotResp ratePlanAllotResp;
    }

    @Data
    public static class RatePlanAllotResp{
        @Element(name = "Errors",required = false)
        private ErrorInfo errorInfo;

        @Element(name = "Success",required = false)
        private String success;

        @Element(name = "AllotReposeSegments",required = false)
        private AllotReposeSegmentInfo allotReposeSegmentInfo;
    }

    @Data
    public static class ErrorInfo{
        @ElementList(entry = "Error",type = ErrorMessage.class, inline = true, required = false)
        private List<ErrorMessage> errorMessages;
    }

    /**接口请求错误提示异常信息*/
    @Data
    public static class ErrorMessage{
        @Attribute(name = "Message")
        private String message;

        @Attribute(name = "Code")
        private String code;
    }

    @Data
    public static class AllotReposeSegmentInfo{
        @ElementList(entry = "AllotReposeSegment",type = AllotReposeSegment.class, inline = true, required = false)
        private List<AllotReposeSegment> allotReposeSegments;
    }

    @Data
    public static class AllotReposeSegment{
        @Attribute(name = "Number")
        private String number;

        @Element(name = "RatePlan")
        private RatePlanResult ratePlanResult;
    }

    @Data
    public static class RatePlanResult{
        @Attribute(name = "RatePlanCode")
        private String ratePlanCode;

        @Attribute(name = "HotelCode")
        private String hotelCode;

        @Element(name = "SearchDateRange")
        private SearchDateRange searchDateRange;

        @Element(name = "AllotmentRange")
        private AllotmentRange allotmentRange;
    }

    @Data
    public static class SearchDateRange{
        @Attribute(name = "Start")
        private String start;
        @Attribute(name = "End")
        private String end;
    }

    @Data
    public static class AllotmentRange{
        @Element(name = "Allotment")
        private Allotment allotment;
    }

    @Data
    public static class Allotment{
        @Element(name = "Text")
        private String text;
    }
}
RatePlanAllotRespEnvelope ratePlanAllotRespEnvelope = persister.read(RatePlanAllotRespEnvelope.class,xml);
System.out.println(JSON.toJSON(ratePlanAllotRespEnvelope));

经过测试调试发现,要把soap:Body修改为Body就能反序列化成功。

修改后结果.png
输出结果:
{"body":{"ratePlanAllotResp":{"allotReposeSegmentInfo":{"allotReposeSegments":[{"number":"1","ratePlanResult":{"searchDateRange":{"start":"2024-08-01","end":"2024-08-30"},"allotmentRange":{"allotment":{"text":"4,4,4,4,4,4,4,4,4,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4"}},"ratePlanCode":"000111010010018051","hotelCode":"10018"}}]}}}}

代码中使用到的jar依赖:

<dependency>
  <groupId>com.squareup.retrofit2</groupId>
  <artifactId>adapter-rxjava</artifactId>
  <version>2.11.0</version>
</dependency>
<dependency>
  <groupId>com.squareup.retrofit2</groupId>
  <artifactId>converter-simplexml</artifactId>
  <version>2.11.0</version>
</dependency>
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.60</version>
 </dependency>

参考文档

1.https://www.jianshu.com/p/b865c855a1e8
2.https://www.jianshu.com/p/e49cf500306e

相关文章

  • Retrofit2+Rxjava2请求WebService(修订

    前言 之前写了一篇文章 Retrofit2+Okhttp3+Rxjava通过SOAP协议请求WebService ...

  • Retrofit2+Okhttp3+Rxjava通过SOAP协议

    前言 刚入职新公司,负责其Android App的开发,他们的接口访问是通过SOAP协议实现的(基于xml数据格式...

  • SOAP协议及Python调用

    1. 什么是SOAP协议  SOAP 是一种简单的基于 XML 的协议,它底层通过 HTTP 来交换信息。S...

  • gsoap --- qq状态查询程序

    一、gSoap简介 1.1、soap协议 介绍gsoap需要先了解soap协议,soap名叫简单对象访问协议,是交...

  • SOAP

    ** 什么是 SOAP? ** SOAP 指简易对象访问协议 SOAP 是一种通信协议 SOAP 用于应用程序之间...

  • 01webService

    IP查询服务的SOAP协议---请求部分 IP查询服务的SOAP协议---响应部分 京东的HTTP协议 WSDL(...

  • 《全栈工程师修炼指南》学习笔记 2

    SOAP 即简单对象访问协议,定义了数据对象传输的格式。 HTTP 协议关心数据的传输过程,而 SOAP 协议则关...

  • SOAP协议

    # SOAP简介# #什么是SOAP SOAP(Simple Object Access Protocol)一般指...

  • SOAPUI及其原理简介

    一、soapUI简介 SOAP: WebService通过Http协议发送请求和接收结果时,发送的请求内容和...

  • API测试基础--SOAP协议

    什么是SOAP SOAP全称Simple Object Access Protocol。它是基于XML协议的WEB...

网友评论

      本文标题:Retrofit2+Okhttp3+Rxjava通过SOAP协议

      本文链接:https://www.haomeiwen.com/subject/zjtwhjtx.html