更新時間:2022-05-13 10:10:56 來源:動力節點 瀏覽3693次
從 Java 1.1 開始,JDK 提供的核心庫中就有一個 HTTP 客戶端。Java 11 添加了一個新的客戶端。如果您對向項目中添加額外的依賴項敏感,其中之一可能是一個不錯的選擇。
Java 1.1 HttpURL連接
以下是您將如何使用它來發出GET請求以獲取 APOD 數據:
// Create a neat value object to hold the URL
URL url = new URL("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY");
// Open a connection(?) on the URL(??) and cast the response(???)
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Now it's "open", we can set the request method, headers etc.
connection.setRequestProperty("accept", "application/json");
// This line makes the request
InputStream responseStream = connection.getInputStream();
// Manually converting the response body InputStream to APOD using Jackson
ObjectMapper mapper = new ObjectMapper();
APOD apod = mapper.readValue(responseStream, APOD.class);
// Finally we have the response
System.out.println(apod.title);
Java 11 HttpClient
HttpClient 接受BodyHandler可以將 HTTP 響應轉換為您選擇的類。有一些內置的處理程序:String,byte[]用于二進制數據,Stream按行分割,還有一些其他的。BodyHandler您也可以定義自己的,這可能會有所幫助,因為沒有用于解析 JSON的內置函數。我已經按照 Java Docs 中的示例基于 Jackson 編寫了一個(此處)。它為APOD 類返回一個,因此我們在需要結果時調用。Supplier.get()
這是一個同步請求:
// create a client
var client = HttpClient.newHttpClient();
// create a request
var request = HttpRequest.newBuilder(
URI.create("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY"))
.header("accept", "application/json")
.build();
// use the client to send the request
var response = client.send(request, new JsonBodyHandler<>(APOD.class));
// the response:
System.out.println(response.body().get().title);
對于異步請求,clientandrequest以相同的方式進行,然后調用.sendAsync而不是.send:
// use the client to send the request
var responseFuture = client.sendAsync(request, new JsonBodyHandler<>(APOD.class));
// We can do other things here while the request is in-flight
// This blocks until the request is complete
var response = responseFuture.get();
// the response:
System.out.println(response.body().get().title);
如果內置客戶端不適合您,請不要擔心!您可以將許多庫引入您的項目中,它們可以完成這項工作。
Apache HttpClient
Apache Software Foundation 的 HTTP客戶端已經存在了很長時間。它們被廣泛使用,并且是許多高級庫的基礎。歷史有點混亂。舊的Commons HttpClient不再開發,新版本(也稱為 HttpClient)在HttpComponents項目下。5.0 版于 2020 年初發布,增加了 HTTP/2 支持。該庫還支持同步和異步請求。
總體而言,API 相當低級 - 您需要自己實現很多。以下代碼調用 NASA API。
ObjectMapper mapper = new ObjectMapper();
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpGet request = new HttpGet("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY");
APOD response = client.execute(request, httpResponse ->
mapper.readValue(httpResponse.getEntity().getContent(), APOD.class));
System.out.println(response.title);
}
OkHttp
OkHttp是Square的 HTTP 客戶端,具有許多有用的內置功能,例如自動處理 GZIP、響應緩存和重試或在網絡錯誤的情況下回退到其他主機以及 HTTP/2 和 WebSocket 支持。盡管沒有內置的 JSON 響應解析,但 API 是干凈的,所以我再次添加了代碼來解析 JSON 與 Jackson:
ObjectMapper mapper = new ObjectMapper();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY")
.build(); // defaults to GET
Response response = client.newCall(request).execute();
APOD apod = mapper.readValue(response.body().byteStream(), APOD.class);
System.out.println(apod.title);
這很好,但是當您在頂部添加 Retrofit 時,OkHttp 的真正威力就很明顯了。
Retrofit
Retrofit是 Square 的另一個庫,建立在 OkHttp 之上。除了 OkHttp 的所有低級特性外,它還添加了一種構建 Java 類的方法,這些類抽象了 HTTP 細節并提供了一個很好的 Java 友好 API。
首先,我們需要創建一個接口來聲明我們想要針對 APOD API 調用的方法,并使用注釋定義這些方法如何對應于 HTTP 請求:
public interface APODClient {
@GET("/planetary/apod")
@Headers("accept: application/json")
CompletableFuture<APOD> getApod(@Query("api_key") String apiKey);
}
返回類型CompletableFuture<APOD>使其成為異步客戶端。Square 提供其他適配器,或者您可以編寫自己的適配器。
在聲明接口之后,我們要求 Retrofit 創建一個實現,我們可以使用它來針對給定的基本 URL 發出請求。能夠切換基本 URL 對集成測試也很有幫助。要生成客戶端,代碼如下所示:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.nasa.gov")
.addConverterFactory(JacksonConverterFactory.create())
.build();
APODClient apodClient = retrofit.create(APODClient.class);
CompletableFuture<APOD> response = apodClient.getApod("DEMO_KEY");
// do other stuff here while the request is in-flight
APOD apod = response.get();
System.out.println(apod.title);
API 認證
如果我們的接口中有多個方法都需要一個 API 密鑰,則可以通過HttpInterceptor在 base 中添加一個來配置它OkHttpClient。可以將自定義客戶端添加到Retrofit.Builder. 創建自定義客戶端所需的代碼是:
private OkHttpClient clientWithApiKey(String apiKey) {
return new OkHttpClient.Builder()
.addInterceptor(chain -> {
Request originalRequest = chain.request();
HttpUrl newUrl = originalRequest.url().newBuilder()
.addQueryParameter("api_key", apiKey).build();
Request request = originalRequest.newBuilder().url(newUrl).build();
return chain.proceed(request);
}).build();
}
構建類來表示遠程 API 是一個很好的抽象,可以很好地與依賴注入配合使用,并且讓 Retrofit 基于可定制的 OkHttp 客戶端為您創建它們非常棒。
如果以上都不是您想要的,請查看以下建議:
REST Assured - 專為測試您的 REST 服務而設計的 HTTP 客戶端。提供用于發出請求的流暢界面和用于對響應進行斷言的有用方法。
cvurl - Java 11 HttpClient 的包裝器,它使您在發出復雜請求時可能會遇到一些尖銳的邊緣。
Feign - 與 Retrofit 類似,Feign 可以從帶注釋的接口構建類。Feign 非常靈活,可以通過多種選項來發出和讀取請求、指標、重試等等。
Spring RestTemplate(同步)和WebClient(異步)客戶端 - 如果您在項目中的其他所有內容中都使用過 Spring,那么堅持使用該生態系統可能是一個好主意。Baeldung 有一篇文章比較它們。
MicroProfile Rest Client - 另一個處于“從帶注釋的接口構建類”模式的客戶端,這個很有趣,因為您也可以重用相同的接口來創建 Web 服務器,并確??蛻舳撕头掌髌ヅ洹H绻跒樵摲諛嫿ǚ蘸涂蛻舳?,那么它可能適合您。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習