【发布时间】:2023-03-31 03:26:01
【问题描述】:
我的本地主机是:http://localhost:8585/api/getproducts,我在 ProductController 中使用 @Requestmapping(/api/getproducts) 来访问我的产品页面。
点击一个按钮,我需要在不同的主机上调用一个 api: http://10.120.130.22:9292/ 我尝试在新控制器中使用以下代码来调用主机:
@RequestMapping("Trainer/reStaff/")
@RequestMapping(method = RequestMethod.POST)
public @ResponseBody response(@RequestParam("trainingId") final int trainingId, HttpServletRequest request)
throws ClientProtocolException, IOException {
String hostname="http://10.120.130.22:9292/";
CloseableHttpClient httpclient = HttpClients.custom().build();
CloseableHttpResponse response=null;
try{
String uri=hostname+"Trainer/reStaff/?trainingId="+trainingId;
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/json");
response = httpclient.execute(httpPost);
String responseData = EntityUtils.toString(response.getEntity());
if(response.getStatusLine().getStatusCode()==200)
System.out.println(responseData+"\n");
else
System.out.println("Error :" + responseData+"\n");
}finally {
httpclient.close();
response.close();
}
但我得到了错误:
HTTP 状态 404 -
类型状态报告
消息
说明请求的资源不可用。
如何从我的控制器调用新主机?
【问题讨论】:
-
从 url String uri=hostname+"Trainer/reStaff?trainingId="+trainingId; 中删除查询参数前多余的 /
-
在浏览器中输入网址是否有效?
-
当我在浏览器中输入 URL 时,出现此错误:{"response":{"error":"Referer Url domain doesn't match with host domain"}}
标签: spring controller request mapping response