Cannot get SOAP envelope body using Retrofit 2 and Simple XML Converter

寵の児 提交于 2019-12-04 06:54:19

I don't know how the Simple XML Converter works exactly, but element names are elements names, and they can be prefixed. This seems to be the confusion because the : character is used to delimit namespace prefixes and element names in physical XML document (tags).

You can just remove the "prefix" from the name and add the @Namespace annotation. For example:

ResponseEnvelope.java

@Root(name = "Envelope")
@Namespace(prefix = "soapenv", reference = "http://schemas.xmlsoap.org/soap/envelope/")
final class ResponseEnvelope {

    @Element(name = "Body", required = false)
    @Namespace(prefix = "soapenv", reference = "http://schemas.xmlsoap.org/soap/envelope/")
    final ResponseBody body;

    private ResponseEnvelope(
            @Element(name = "Body") final ResponseBody body
    ) {
        this.body = body;
    }

}

ResponseBody.java

@Root(name = "Body", strict = false)
@Namespace(prefix = "soapenv", reference = "http://schemas.xmlsoap.org/soap/envelope/")
final class ResponseBody {

    @Element(name = "autenticarUsuarioPorEmailResponse", required = false)
    @Namespace(prefix = "ns", reference = "http://business.curitiba.org")
    final ResponseData requestData;

    private ResponseBody(
            @Element(name = "autenticarUsuarioPorEmailResponse") final ResponseData requestData
    ) {
        this.requestData = requestData;
    }

}

ResponseData.java

@Root(name = "autenticarUsuarioPorEmailResponse", strict = false)
@Namespace(prefix = "ns", reference = "http://business.curitiba.org")
final class ResponseData {

    @Element(name = "return", required = false)
    @Namespace(prefix = "ns", reference = "http://business.curitiba.org")
    final ResponseInfo info;

    private ResponseData(
            @Element(name = "return") final ResponseInfo info
    ) {
        this.info = info;
    }

}

ResponseInfo.java

@Root(name = "return", strict = false)
@Namespace(prefix = "ns", reference = "http://business.curitiba.org")
final class ResponseInfo {

    @Element(name = "tokenAcesso", required = false)
    @Namespace(prefix = "ax2471", reference = "http://saidas.curitiba.org/xsd")
    final String accessToken;

    @Element(name = "idCredencial", required = false)
    @Namespace(prefix = "ax2471", reference = "http://saidas.curitiba.org/xsd")
    final String credentialId;

    private ResponseInfo(
            @Element(name = "tokenAcesso") final String accessToken,
            @Element(name = "idCredencial") final String credentialId
    ) {
        this.accessToken = accessToken;
        this.credentialId = credentialId;
    }

}

So the following example:

final ResponseInfo info = responseEnvelope.body.requestData.info;
System.out.println(info.accessToken);
System.out.println(info.credentialId);

will output:

635E3DA9-7C02-4DB7-9653-E7688C66B02C
3282

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!