Java 如何将http响应放入一个集合中

Java 如何将http响应放入一个集合中,java,rest,http,set,response,Java,Rest,Http,Set,Response,如何将http响应放入员工类型的集合(集合集合) 我以HTTP响应的形式获取数据,但我必须将其放入雇员类型的集合(集合集合)中 public Set getAllEmployees()引发ServiceException{ setOtherAppDetails(); HttpStatus status=HttpStatus.OK; Set employees=new HashSet(); 试一试{ System.out.println(“内部新创建的DNG控制器方法”); 字符串姿势=”http

如何将http响应放入员工类型的集合(集合集合)

我以HTTP响应的形式获取数据,但我必须将其放入雇员类型的集合(集合集合)中

public Set getAllEmployees()引发ServiceException{
setOtherAppDetails();
HttpStatus status=HttpStatus.OK;
Set employees=new HashSet();
试一试{
System.out.println(“内部新创建的DNG控制器方法”);
字符串姿势=”http://localhost:8081/otherApp/empserv/list/dngEmployees";
HttpClient HttpClient=HttpClientBuilder.create().build();
HttpPost=新的HttpPost(postrl);
HttpResponse response=httpClient.execute(post);
HttpEntity=response.getEntity();
System.out.println(“responseBody=“+EntityUtils.toString(entity));
//employees.add((Employee)response);想做类似的事情,但它不起作用,如果它看起来很愚蠢,请道歉
}捕获(IOException异常){
异常。printStackTrace();
status=HttpStatus.INTERNAL\u SERVER\u错误;
}       
返回员工;
}

响应应该保存在Set中

您可以使用Jackson库将json解析为Set

如果使用Maven,请首先导入


com.fasterxml.jackson.core

您得到的答复的格式是什么?它看起来像什么?“id:“5”,“text:”null,“category:”Employees”-它可能包含更多的数据。您在HttpResponse中得到的内容类型头的值是多少?是的,它是json类型您可以从这里开始:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.9.2</version>
</dependency>
ObjectMapper mapper = new ObjectMapper();
Set<Employee> myObjects = mapper.readValue(EntityUtils.toString(entity), new TypeReference<Set<Employee>>(){});