将输入流转换为对象列表
Convert inputstream to list of Objects
我必须制作一个 jar 来点击 API 以获得基本上有四个字段的人员详细信息列表:- id、name、salary、department。
我正在使用 apache httpclient 做一个 get 请求,它在点击 API.
时给我一个 httpentity
httpentity 提供了获取响应内容的方法,但是它 returns inputstream.
在通过 inputreader 读取此输入流并将其打印出来时,我确认它正在为我提供一个 persondetails 列表。
但我不知道如何将其转换为人员详细信息列表。
那是 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();
}
}
我想将此内容转换为人员详细信息列表。
有人能帮帮我吗?
我听说过杰克逊,但我不知道如何使用它。
首先我创建一个默认集合类型
CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(List.class, PersonDetails.class);
List<PersonDetails> personDetails = mapper.readValue(content,collectionType);
其中content是inputStream。
我在某人的博客上找到的。
我必须制作一个 jar 来点击 API 以获得基本上有四个字段的人员详细信息列表:- id、name、salary、department。
我正在使用 apache httpclient 做一个 get 请求,它在点击 API.
时给我一个 httpentityhttpentity 提供了获取响应内容的方法,但是它 returns inputstream.
在通过 inputreader 读取此输入流并将其打印出来时,我确认它正在为我提供一个 persondetails 列表。
但我不知道如何将其转换为人员详细信息列表。
那是 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();
}
}
我想将此内容转换为人员详细信息列表。
有人能帮帮我吗? 我听说过杰克逊,但我不知道如何使用它。
首先我创建一个默认集合类型
CollectionType collectionType = mapper.getTypeFactory().constructCollectionType(List.class, PersonDetails.class);
List<PersonDetails> personDetails = mapper.readValue(content,collectionType);
其中content是inputStream。 我在某人的博客上找到的。