【发布时间】:2016-10-31 13:17:22
【问题描述】:
我试图在我的 Spring RESTful 应用程序中使用来自 http://www.foaas.com/ 的 JSON 对象。但是,它们需要使用 Accept 标头进行内容协商。
我已经尝试过在 SO 上找到的这种方法:
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
HttpEntity entity = new HttpEntity(headers);
HttpEntity response = restTemplate.exchange(foaasURI, HttpMethod.GET, entity, String.class);
我每次尝试都被 403 禁止。
使用 Postman 添加 Accept 标头可以正常工作,因此我知道在 foaas 结束时一切都是正确的。我该如何进行?
【问题讨论】:
-
ResponseEntity<Object> response = restTemplate.exchange("http://foaas.com/off/Tom/Everyone?shoutcloud&i18n=de", HttpMethod.GET, null, Object.class);它对我来说工作正常。您可以发布您尝试访问的 URL 吗?您使用的是哪个版本的 Spring? -
我使用的是 Spring 4.0.0。即使以您的示例为例,我也被 403 禁止。这很奇怪,因为 foaas 是完全开放的。
-
我正在查看我的测试代码,我注意到它正在工作,因为我使用的是 HttpComponentsClientHttpRequestFactory 而不是 SimpleClientHttpRequestFactory。让我向内看一下,看看到底发生了什么。
标签: java spring http-headers resttemplate spring-rest