首页 > 解决方案 > 非法字符集作为回应。无法使用 Spring Rest 模板解析特定的 SOAP XML 响应

问题描述

在将 RestTemplate 用于 XML 响应时,我在响应中收到不可读的字符。下面是我的 resttemplate 配置。

RestTemplate restTemplate = new RestTemplateBuilder().basicAuthentication(userName, userPassword).build();
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        stringHttpMessageConverter.setWriteAcceptCharset(true);
        for (int i = 0; i < restTemplate.getMessageConverters().size(); i++) {
            if (restTemplate.getMessageConverters().get(i) instanceof StringHttpMessageConverter) {
                restTemplate.getMessageConverters().remove(i);
                restTemplate.getMessageConverters().add(i, stringHttpMessageConverter);
                break;
            }
        }

这就是我得到的回应。

��]o�0��
ʤ�%�'   ��eT�T:t�.�s��;�M)�~�h�M�s|��|;x�RW�g�� �Y��Z�T��o2�ayc'�8Nj/g���k:��P�����
������_���g�*Ϫ�n��컮��w���_0�;l3�4���+��0�Ufm%O�����Һ��'Q�s̕wa��jW�y0�7<H��n`�I��1-7����mch���0�J�V�at4�xk�&amp;i(D  W���Tb��b�����%F�   r\r�c5�=�8r]�
                                                                                                                                                     0����"�F�=ǟ��v'���������T�XS���"}��
��b�����̔sI�]T3"I����mKߑ��;�5:�զƓM�J���K�x�8\G$�(ţp����p�i4
                                                          W�o��8���
                                                                   ���n>L�a��O�rHFe$�O�$.�� ���f���[J!��-8lL[��ޡ��E�&lt;�Dۛ[P�v��c^�ㇱ� u�qr\qsx�y��3B�~P�OF���

我能够解析的 Resonse 是:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="urn:creditCard" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <SOAP-ENV:Body>
            <ns1:creditCardResponse xmlns:ns1="urn:creditCard"><return xsi:type="tns:RPResponse">
            <Status xsi:type="xsd:int">0</Status>
            <ReferenceCode xsi:type="xsd:string">#xxxxx</ReferenceCode>
            <EligibleCard xsi:type="xsd:string"/>
            <Errorcode xsi:type="xsd:int">6</Errorcode>
            <Errorinfo xsi:type="xsd:string">Duplicate application</Errorinfo>
            <RequestIP xsi:type="xsd:string">xx.xx.xx.xx</RequestIP>
            </return></ns1:creditCardResponse>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

我无法解析的响应是:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:creditCard">
   <SOAP-ENV:Body>
      <ns1:creditCardResponse xmlns:ns1="urn:creditCard">
         <return xsi:type="tns:RPResponse">
            <Status xsi:type="xsd:int">1</Status>
            <ReferenceCode xsi:type="xsd:string">#xxxxxx</ReferenceCode>
            <CreditLimit xsi:type="xsd:string">75000</CreditLimit>
            <NTCFlag xsi:type="xsd:string"/>
            <DecisionSegment xsi:type="xsd:string"/>
            <EligibleCard xsi:type="xsd:string">0</EligibleCard>
            <ResParam1 xsi:type="xsd:string">http://xxx-xx.xxxxxx.xxx/xxx-xx-xxx?CustomerToken=xxx&amp;E=xxx</ResParam1>
            <Errorcode xsi:type="xsd:int">0</Errorcode>
            <Errorinfo xsi:type="xsd:string"/>
            <RequestIP xsi:type="xsd:string">xx.xx.xx.xx</RequestIP>
            <MobileMatchFlag xsi:type="xsd:string">Y</MobileMatchFlag>
         </return>
      </ns1:creditCardResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

尝试了很多东西,这是我提出请求的最终代码。

HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE, "text/xml;charset=UTF-8");
        headers.add("SOAPAction", "urn:creditCard#creditCard");
        headers.set("Accept", "text/xml;charset=UTF-8");
        headers.set("Accept-Language", "en-GB");
        headers.set("Content-Language", "en-GB");
        headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8")));


        HttpEntity<String> requestEntity = new HttpEntity<String>(xmlText, headers);

//      RequestEntity<String> soapRequest = null;
//      try {
//          soapRequest = RequestEntity.post(new URI(defaultUri)).contentType(MediaType.TEXT_XML)
//                  .accept(MediaType.TEXT_XML).acceptCharset(Charset.forName("UTF-8"))
//                  .header("SOAPAction", "urn:creditCard#creditCard").body(xmlText);
//      } catch (URISyntaxException e) {
//          throw new BaseException("Service is Unavailable at this Moment. Please check later!!");
//      }
        ResponseEntity<String> response = null;
        try {
            response = restTemplate.exchange(new URI(defaultUri), HttpMethod.POST, requestEntity, String.class);
log.info("Succesfully Response Recieved ....Credit Limit API RAW Response  : " + response.getBody());
......

标签: javaxmlspring-bootsoapresttemplate

解决方案


推荐阅读