如何将http响应放入员工类型的集合(集合集)中。
我得到数据作为HTTP响应,但我必须把它放在一个集合(集合集)的雇员类型
public Set<Employee> getAllEmployees() throws ServiceException {
setOtherAppDetails();
HttpStatus status = HttpStatus.OK;
Set<Employee> employees = new HashSet<Employee>();
try {
System.out.println("Inside newly created DNG controller method");
String postUrl = "http://localhost:8081/otherApp/empserv/list/dngEmployees";
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(postUrl);
HttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
System.out.println("responseBody =" +EntityUtils.toString(entity));
// employees.add((Employee)response); wants to do something like this but its not working, sorry if it seems silly
} catch (IOException exception) {
exception.printStackTrace();
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
return employees;
}反应应保持在一定的范围内。
发布于 2019-08-01 17:14:06
您可以使用库Jackson解析json作为Set。
如果使用Maven,首先导入依赖性
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9.2</version>
</dependency>然后您可以解析http响应,如下所示:
ObjectMapper mapper = new ObjectMapper();
Set<Employee> myObjects = mapper.readValue(EntityUtils.toString(entity), new TypeReference<Set<Employee>>(){});对象雇员需要准确地表示响应。
更多示例这里
https://stackoverflow.com/questions/57313175
复制相似问题