Java SpringRESTTemplateGet请求返回302状态

Java SpringRESTTemplateGet请求返回302状态,java,spring,rest,api,http,Java,Spring,Rest,Api,Http,我试图使用Spring的RestTemplate组件使用第三方RESTAPI。我尝试在外部RESTAPI客户机(Postman)上输入相同的请求-使用相同的URI和自定义头,我能够检索正确的数据 但是,当我尝试使用RestTemplate镜像确切的请求时,它返回我 <html><head> <title>302 Found</title> </head><body>

我试图使用Spring的RestTemplate组件使用第三方RESTAPI。我尝试在外部RESTAPI客户机(Postman)上输入相同的请求-使用相同的URI和自定义头,我能够检索正确的数据

但是,当我尝试使用RestTemplate镜像确切的请求时,它返回我

        <html><head>
        <title>302 Found</title>
        </head><body>
        <h1>Found</h1>
        <p>The document has moved <a href="https://address/{path of endpoint}">here</a>.</p>
        <hr>
        <address>Apache/2.4.7 (Ubuntu) Server at address Port 80</address>
        </body></html>

302发现
建立
文档已移动


地址端口80的Apache/2.4.7(Ubuntu)服务器
这是我正在使用的代码示例:

        String uri = "http://address/{path of endpoint}";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.set(someCustomHeaderKey, someCustomHeaderValue);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
stringuri=”http://address/{端点路径}”;
RestTemplate RestTemplate=新RestTemplate();
HttpHeaders=新的HttpHeaders();
headers.set(someCustomHeaderKey,someCustomHeaderValue);
HttpEntity=新的HttpEntity(标题);
ResponseEntity response=restemplate.exchange(uri,HttpMethod.GET,entity,String.class);

我读到java不允许从一个协议重定向到另一个协议,例如从http重定向到https,反之亦然。在这方面需要一些帮助。

默认情况下,RestTemplate将遵循重定向,但如果协议不同, 这就是您看到的情况(从http重定向到https)

有关更完整的解释以及实现此功能的代码,请参阅

我在本地机器上试用了你的代码,一切都很好。 302状态代码表示您的URI位置不同。 根据您的示例,您应该在URI中使用https而不是Http

我已经试过你的代码如下

String uri = "https://jsonplaceholder.typicode.com/todos/1";
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("link", "http/:");
    HttpEntity<String> entity = new HttpEntity<String>(headers);
    ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, 
    entity, String.class);
    System.out.println(response);
stringuri=”https://jsonplaceholder.typicode.com/todos/1";
RestTemplate RestTemplate=新RestTemplate();
HttpHeaders=新的HttpHeaders();
headers.set(“link”,“http/:”);
HttpEntity=新的HttpEntity(标题);
ResponseEntity response=restemplate.exchange(uri,HttpMethod.GET,
实体、字符串、类);
System.out.println(响应);
控制台中的输出

<200,{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
},[Date:"Thu, 12 Mar 2020 03:45:44 GMT", Content-Type:"application/json; charset=utf-8", Content-Length:"83", Connection:"keep-alive", Set-Cookie:"__cfduid=d3104b8bbd25cbcb802977fc9183d559e1583984744; expires=Sat, 11-Apr-20 03:45:44 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax", X-Powered-By:"Express", Vary:"Origin, Accept-Encoding", Access-Control-Allow-Credentials:"true", Cache-Control:"max-age=14400", Pragma:"no-cache", Expires:"-1", X-Content-Type-Options:"nosniff", Etag:"W/"53-hfEnumeNh6YirfjyjaujcOPPT+s"", Via:"1.1 vegur", CF-Cache-Status:"HIT", Age:"1747", Accept-Ranges:"bytes", Expect-CT:"max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"", Server:"cloudflare", CF-RAY:"572a862eab83d5e8-BOM"]>

所以。。。在您的
uri
中放入
https