我调用我的 https REST APi,它使用 Postman 在另一台服务器上调用 http api,它返回 502 bad gateway 错误。 https API 的身份验证在 header 中设置,并接受:application/json 。所有其他不调用远程服务器上的 http api 的 https 其余 api 都可以正常工作。
我使用 SpringBoot 创建了带有身份验证的 REST API。有 GET 和 POST 方法。它们都有类似 https://... 的 URL。在调用它们之前,用户必须获得安全 token 。这些 Rest api 可作为 war 文件使用,驻留在部署在 tomcat 中的 Server_1 上。从许多 API 中,我调用另一个 Rest 服务,该服务作为 jar 文件驻留在另一台 Server_2 上,其 URL 的 URL 类似于 http://...。当我调用驻留在 Server_1 上的 war 文件中的一个 https Rest api 时,请求到达 Server_2。 Server_2 上的 Http Rest api 使用 JSON 文件进行应答。我可以在 Server_2 的日志中看到它,而且我还捕获了服务器之间的 http 流量。看起来 http Rest api 收到请求并将答案发回,但来自 Server_1 的 https RestApi 拒绝答案,因为它未加密并给出错误的网关 502 错误。有什么办法可以修复这个错误吗?
//这就是我在服务器2上调用http Rest api的方式。此代码是服务器1上https Rest api的一部分(tomcat中的war文件)
public ResponseEntity<String> function()
{
RestTemplate restTemplate = new RestTemplate();
String myUrl = "http://xxxxxx:8081";
ResponseEntity<String> response = restTemplate.getForEntity(myUrl + "/api/test/suspend/12", String.class);
return responce
}
最佳答案
您的代码中有一些小问题 最后一行(返回语句)需要以分号结束,并且您写的是“responce”而不是“response”。
public ResponseEntity<String> function()
{
RestTemplate restTemplate = new RestTemplate();
String myUrl = "http://xxxxxx:8081";
ResponseEntity<String> response = restTemplate.getForEntity(myUrl + "/api/test/suspend/12", String.class);
return response;
}
您应该额外使用HTTP请求的响应内容并将其用作您实际的HTTPS请求的内容,否则您将混合请求而无法工作。
关于java - 从 httpsrest api 调用 httprestapi 时出现错误 502,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57597895/