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就能反序列化成功。
输出结果:
{"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