如何从 HttpResponse 返回哈希映射?
我是 Java 新手。我正在尝试访问外部 API,但有点不知道如何将响应转换回哈希图。我在网上看到的与 API 交互的所有示例都假设您希望将其作为字符串返回,或者您想要将其映射回现有的 POJO。我实际上只关心在响应中提取值。在下面的示例中,我尝试从 API 中提取访问令牌(稍后,在我尚未定义的方法中,使用所述访问令牌从不同端点检索信息)。
如果有一个库需要更少的样板文件,我也不会执着于 Java 11 的内置库。
我有点惊讶的是,我的谷歌搜索都没有显示返回哈希图的 API 消费示例……正在转换为 Java 的自定义 POJO 标准吗?当我不关心很多 JSON 字段时,感觉就像有很多不必要的样板。
(此外,如果此片段还有其他问题,我愿意接受建议!)
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class AlationApiClient {
private static final HttpClient client = HttpClient.newBuilder().build();
private static final String alationUrl = "someUrl";
private String refreshToken;
private Integer userId;
private String accessToken;
public AlationApiClient(String refreshToken, Integer userId) {
this.refreshToken = refreshToken;
this.userId = userId;
}
private static String createRequestBody(Map<String, Object> data) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(data);
}
public Object getAccessToken(String refreshToken, Integer userId) throws JsonProcessingException {
String url = alationUrl + "/integration/v1/createAPIAccessToken/";
Map<String, Object> data = Map.of(
"refresh_token", this.refreshToken,
"user_id", this.userId
);
String requestBody = createRequestBody(data);
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
return AlationApiClient.client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(???) // what do I do here to put things into a hashmap??
.join();
}
}
I am new to Java. I am trying to hit an external API and am a little lost of how I translate the response back to a hashmap. All the examples I see online for interacting with APIs assume you want it back as a string or have an existing POJO you want to map things back to. I literally just care about extracting values in the response. In the below example, I am trying to extract out the access token from an API (and, later, in methods I haven't defined yet, use said access token to retrieve information from different endpoints).
I'm not wedded to Java 11's built-in library, either, if there is a library that requires less boilerplate.
I'm a little surprised none of my Googling has surfaced examples of API consumption where a hashmap is returned... Is converting into a self-defined POJO standard for Java? Feels like a lot of unnecessary boilerplate when I don't care about a lot of the JSON's fields.
(Also, if there is anything else wrong with this snippet, I'm open to suggestions!)
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class AlationApiClient {
private static final HttpClient client = HttpClient.newBuilder().build();
private static final String alationUrl = "someUrl";
private String refreshToken;
private Integer userId;
private String accessToken;
public AlationApiClient(String refreshToken, Integer userId) {
this.refreshToken = refreshToken;
this.userId = userId;
}
private static String createRequestBody(Map<String, Object> data) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper
.writerWithDefaultPrettyPrinter()
.writeValueAsString(data);
}
public Object getAccessToken(String refreshToken, Integer userId) throws JsonProcessingException {
String url = alationUrl + "/integration/v1/createAPIAccessToken/";
Map<String, Object> data = Map.of(
"refresh_token", this.refreshToken,
"user_id", this.userId
);
String requestBody = createRequestBody(data);
HttpRequest request = HttpRequest.newBuilder(URI.create(url))
.POST(HttpRequest.BodyPublishers.ofString(requestBody))
.build();
return AlationApiClient.client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(???) // what do I do here to put things into a hashmap??
.join();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论