【发布时间】:2018-07-25 22:06:43
【问题描述】:
我必须制作一个 jar 来访问 API 以获取人员详细信息列表,该列表基本上有四个字段:- id、name、salary、department。
我正在使用 apache httpclient 执行获取请求,该请求在访问 API 时为我提供了 httpentity。
httpentity 提供了一种获取响应内容的方法,但它返回的是输入流。
通过 inputreader 读取此输入流并将其打印出来,我确认它正在给我一个人员详细信息列表。
但我不知道如何将其转换为人员详细信息列表。
这是 PersonDetails 对象:-
public class PersonDetails {
private UUID id;
private String name;
private String department;
private Integer salary;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
@Override
public String toString() {
return "PersonDetails{" +
"id=" + id +
", name='" + name + '\'' +
", department='" + department + '\'' +
", salary=" + salary +
'}';
}
}
这是我的 GetRequest 代码:-
public static void main(String[] args) {
List<PersonDetails> personDetails = new ArrayList<>();
HttpClient httpClient = HttpClientBuilder.create().build();
String url = "/api/person-details";
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream content = httpEntity.getContent();
} catch (IOException e) {
e.printStackTrace();
}
}
我想将此内容转换为人员详细信息列表。
有人可以帮助我吗? 我听说过Jackson,但我不知道如何使用它。
【问题讨论】:
-
看看这个问题的答案:stackoverflow.com/a/18795009/13075
标签: list jackson inputstream apache-httpclient-4.x