【发布时间】:2022-06-29 16:28:18
【问题描述】:
我正在调用 Jasper 服务器 API 端点之一,我必须将标头“Accept”设置为“application/json”,以便服务返回 JSON 响应。我已经验证了 Postman 的 API -
当我尝试从我的 Spring Boot 休息客户端模拟相同的行为时,我尝试将接受标头设置为“应用程序/json”,但 Spring 似乎忽略了这一点并添加了接受标头,如下所示 -
我已经通过使用以下参数为休息模板启用 DEBUG 来验证相同的 - logging.level.org.springframework.web.client.RestTemplate=DEBUG
下面是我的 rest 客户端的代码 sn-p -
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBasicAuth(serviceUsername, servicePassword, StandardCharsets.UTF_8);
ResponseEntity<String> response = null;
String url = serviceEndpoint + "?reportUnitURI="
+ URLEncoder.encode(reportPath, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20")
+ "&label=" + URLEncoder.encode(label, StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20");
LOGGER.info("URL : " + url);
HttpEntity<String> requestEntity = new HttpEntity<String>("",
headers);
response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
- 有人可以帮忙解释一下这里的行为吗?
- 为什么我的 'accept' 标头值会被忽略?
- 如何正确传递“接受”标头?
【问题讨论】:
标签: java spring-boot rest http jasperserver