簡體   English   中英

調用RESTful Web服務

[英]Invoking RESTful webservices

當我們嘗試使用客戶端代碼調用RESTful Web服務時,消費者在實際的開發和生產環境中是否總是必須在物理上提到URL?

public class JaxRSClient {
    public static void main(String args[]){
        //Creating Client
        Client client=ClientBuilder.newClient();

        //Setting the target on client  
        WebTarget wtarget=client.target("http://localhost:8080/jersey/RestWebService/HelloREST/service");

        //Getting response          
        System.out.println(wtarget.request(MediaType.TEXT_PLAIN).get(String.class));
    }
}

最好將URL保留在包含應用程序配置參數的配置文件中。 將其注入客戶端文件並調用API。

優點

  1. 易於更改和維護網址。
  2. 您不必在每次更改URL時都重新構建應用程序。
  3. 隱藏敏感信息,例如應用程序密鑰。

您應該有一個全局屬性文件,其中將保留所有可配置的應用程序常量,並確保有可用的邏輯將更新的全局屬性獲取到您的應用程序。

暫無
暫無

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

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