【问题标题】:Calling a different host from Spring Controller从 Spring Controller 调用不同的主机
【发布时间】: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


【解决方案1】:

我明白这是如何工作的。我们需要在服务层通过 httpPost 传递 url:

HttpPost httpPost = new HttpPost(hostUri); JsonObject jsonResponse = null;

    try {
        String httpRequestBody = jsonRequestBuilder.build().toString();
        logger.info("Request Body: " + httpRequestBody);

        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connManager).build();
        httpPost.setHeader("Content-Type", "application/json");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setEntity(new StringEntity(httpRequestBody));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        logger.debug("Response Status: " + httpResponse.getStatusLine().getStatusCode());
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        String line;
        StringBuffer httpResponseBody = new StringBuffer();
        while ((line = reader.readLine()) != null) {
            httpResponseBody.append(line);
        }
        logger.info("Response Body: " + httpResponseBody.toString());
        JsonReader jsonReader = Json.createReader(new StringReader(httpResponseBody.toString()));
        jsonResponse = jsonReader.readObject();
        jsonReader.close();
    } catch (Exception ex) {
        logger.error("Error occurred while invoking POST on ep: " + hostUrl, ex);
    } finally {
        httpPost.releaseConnection();
    }
    logger.debug("Exiting");
    return jsonResponse;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 2012-05-20
    • 2015-02-10
    • 2021-08-19
    • 2020-10-27
    • 2014-05-12
    相关资源
    最近更新 更多