Spring Boot RestTemplate WebClient-响应正文JSON空

问题描述 投票:0回答:1

我正在使用Rest Template,也已经使用了Web Client。我得到了响应的钥匙,但身体始终是空的。使用邮递员,我可以看到响应,它是一个JSON。

我的代码段在下面-

    HttpHeaders headers = new HttpHeaders();
    headers.setBasicAuth("john", "doe");

    HttpEntity<String> request = new HttpEntity<String>(headers);

    ResponseEntity<String> response = restTemplate.exchange(
            "https://www.getMeData.com",
            HttpMethod.GET, request, String.class);

    System.out.println(response.getBody());

Response如下-

{“结果”:[]}

有人遇到过同样的问题吗?

在邮递员中,我看到的响应标题是-

KEY VALUE

内容编码gzip

Content-Type application / json; charset = UTF-8

传输编码分块

spring-boot httpresponse resttemplate spring-webclient
1个回答
0
投票

尝试添加标题如下:

HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth("john", "doe");
headers.setContentType(MediaType.APPLICATION_JSON);

 HttpEntity<String> request = new HttpEntity<String>(headers);

 For gZip - hope this works

httpHeaders.add(HttpHeaders.CONTENT_ENCODING, "gzip");
httpHeaders.add(HttpHeaders.ACCEPT_ENCODING, "gzip");
© www.soinside.com 2019 - 2024. All rights reserved.