簡體   English   中英

如何從ResourceMapping轉發到同一服務器上的新URL?

[英]How to forward to a new URL on same server from ResourceMapping?

我已經定義了portlet控制器響應映射,如下所示:

@ResourceMapping("create")
public String create(ResourceRequest request, ResourceResponse response){

    return "test"

}

我正在嘗試使用以下命令向另一個服務發出新請求:

@ResourceMapping("create")
public String create(ResourceRequest request, ResourceResponse response){

    new HttpGet("/path-to-my-service/myService");

    return "test"

}

沒有顯示任何錯誤,但我認為Get請求未正確調用,因為我無權訪問該服務所駐留的域名。 可以使用ResourceRequest / ResourceResponse訪問嗎? 查看此obj的屬性,這些屬性似乎不可用。 我還能嘗試調用Get請求嗎?

我可以通過提供完整路徑來使其在瀏覽器上工作:

http://localhost:8080/path-to-my-service/myService但是我需要從@ResourceMapping內部解析服務的域嗎?

沒有錯誤顯示

它不應該,此代碼不執行任何操作

new HttpGet("/path-to-my-service/myService");

我想您嘗試使用http客戶端調用外部服務,您必須提供完整的http調用代碼才能獲取它

這很適合http客戶端4.3的示例

    public static String executeGet(String url) {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resp = null;  
        try {

            HttpGet httpGet = new HttpGet(url);
            response = httpClient.execute(httpGet);

            int code = response.getStatusLine().getStatusCode();
            if (code == HttpURLConnection.HTTP_OK) {
                resp = EntityUtils.toString(response.getEntity());
            } else {
                // handle wrong response here
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                close(response);
            }
        }
        return resp;
}

您應該提供此方法的完整URL,這意味着您應該知道主機名。 如果您知道您的Portlet和服務在同一主機上,那么您可以從ServletRequest獲取主機名,那么每個門戶網站實現都提供從PorteltRequest獲取HttpServletRequest的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM