简体   繁体   English

Java-Spring:REST GET 请求在正文中返回错误

[英]Java-Spring: REST GET request returns something wrong in body

I am trying to get Json from this url: https://meduza.io/api/v3/search?chrono=news&page=0&per_page=10&locale=ru with pretty simple code:我试图从这个 url 获取 Json: https ://meduza.io/api/v3/search?chrono = news&page =0& per_page =10& locale =ru使用非常简单的代码:

public Boolean getData(String apiUrl) {
        try {
            //disable SSLCertificateValidation
            SSLCertificateValidation.disable();

            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json;charset=UTF-8");
            headers.add("Accept", "*/*");
            
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiUrl, String.class, headers);

            if (responseEntity.getStatusCode().toString().equals("200")) {
                theLogger.info("Server successfully answered with response code: {} - {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
                return true;
            } else {
                theLogger.error("Something goes wrong! Server answered with response code: {} and error: {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
                return false;
            }

        } catch (Exception theException) {
            theException.printStackTrace();
            theLogger.error("Exception: " + theException);
            return false;
        }

    }

It works but I see this in the body:它有效,但我在身体中看到了这一点:

Server successfully answered with response code: 200 - ��������Y�n�}B� DiF�d�@P�(v����E�]�J�5CNH��b�<Fh_.O�s93��*v]���cs�s/��!�k'5y.S����ԑ�����Q<�Gq0���9%4/�q2����+��Ϋ�𒧹P��~!<�����[�IL[K+���K������J�Eb*�]nV�ݒ;��*U3�y��t���27+iu!5$�Fx�G|����ϰ#a��^T9�x"Ty�Q�V��ܕf)]�Lj%�-���7�PL8'�6��%��~��
����`�T��F;��D���
�F��3��~A����&�+a����xc'X���
��\�
Y�V��5�L

O��0|�fP�Ⱦ<��`@;s��d)�u~�u�9����ngV������ʁ���~�^��F��OM(�l��`E�����^�'G�h��*�cX�E<>�I#,��Q�U���>V&l�4[+��&BkIªF��Ze�~�dnPKe/Z��俉�t���x�j(6�݄b;P�
|cA�    ��΢;�Oի�=$�3:{g��L�G�U��* az���x�`E,��Q��)�̅4%t���f�W�K�$������}�1���Kl�SjV;C�9Ė�DU�oi|��M;��$-@��'�.��GyG���}0w|���|=����3I8����f-0��,������l�[��g��dr�V?.���p<�y�`�m_7x�A�鱯�n��ǿ�k��C+    G�F��J
�&�Nlo8RIq�J@���[\����Y�$�� ��r{X�+��2\�����l���sUȆ���OZ��E�!D�hi�N�L��G��6~-�T����_Azm��s:x��ķ��h��
���[p��1�~ ٭����J���f���۝��1���k����s�R��{U��`�c��et����=��R�.A��Q���{M�Ee���D���Jq��ROƏ�j���2�~�Lb���~Z��F��{p���T��~E���nG�~X���r�x?|�M�����d����e9�~`�sy/а�v��b���(�����N�x4���o��   ?�F����ew��2��[�Yv��ǣi|�4�n�OEY�]t�+�U(�ƛSv!�M��`}��?ޥB���A)�!�mo#>��s۽x(im��*b�����ī�W�C��a����G$BͻӖ]v�f"N�g�#͆�L�o>yc_���?�
DMtꭘ�T�l�Ӹ��b����LցL�?9��������w����.�.K��J��gV�/�O���鮦����~�ZT��]��a
h�i`��z��;p'��X�U��g��`�&mA�`�<Ws��a��#�C��t�K2t��z",���
����ƛ&�"N`\��C�=`�B8�������oFJ%��

What is this?这是什么? What did I do wrong?我做错了什么?

The response is compressed (gzipped) by the server.响应由服务器压缩(gzipped)。 Most browsers/tools handle compressed responses transparently.大多数浏览器/工具都透明地处理压缩响应。 Here you have to do it explicitly.在这里你必须明确地做到这一点。

To uncompress the response you can use GZipInputStream .要解压缩响应,您可以使用GZipInputStream

ResponseEntity<byte[]> response = restTemplate.getForEntity(apiUrl, byte[].class);
GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(response.getBody()));

// convert the stream to string
StringWriter writer = new StringWriter();
IOUtils.copy(gzip, writer, "UTF-8");
String responseString = writer.toString();

Stream to string conversion uses Apache Commons-IO .流到字符串的转换使用Apache Commons-IO

It seems that the server always uses compression - even if the client does not tell it to do so (by sending the accept-encoding header).服务器似乎总是使用压缩——即使客户端没有告诉它这样做(通过发送accept-encoding标头)。

Examine the value of the Content-Encoding header to test if the response is compressed or not.检查Content-Encoding标头的值以测试响应是否被压缩。

response.getHeaders().get("Content-Encoding"); // contains [gzip] if response is compressed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM