我有一個調用休息服務的應用程序。我需要給它傳遞一個URL,現在我要通過串聯一個字符串來創建URL。我這樣做是這樣的:String urlBase = "http:/api/controller/"; String apiMethod = "buy";String url = urlBase + apiMethod;上面顯然是假的,但是重點是我正在使用簡單的字符串concats。這是最佳做法嗎?我對Java比較陌生。我應該建立一個URL對象嗎?
3 回答

Smart貓小萌
TA貢獻1911條經驗 獲得超7個贊
如果您使用的是jersey-client。以下是在不使代碼變得丑陋的情況下訪問子資源的最佳實踐
資源:/ someApp
子資源: / someApp / getData
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target("https://localhost:7777/someApp/").path("getData");
Response response = webTarget.request().header("key", "value").get();
添加回答
舉報
0/150
提交
取消