如何將 http 響應(yīng)放入 Employee 類型的集合(Collection Set)中。我正在獲取數(shù)據(jù)作為 HTTP 響應(yīng),但我必須將其放入員工類型的集合(集合集)中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;}響應(yīng)應(yīng)保存在 Set 中
1 回答

弒天下
TA貢獻1818條經(jīng)驗 獲得超8個贊
您可以使用庫 Jackson 將 json 解析為 Set。
如果你使用 Maven 首先導(dǎo)入依賴
<dependency> ????<groupId>com.fasterxml.jackson.core</groupId> ????<artifactId>jackson-databind</artifactId> ????<version>2.9.9.2</version> </dependency>
然后你可以解析 http 響應(yīng)如下:
ObjectMapper?mapper?=?new?ObjectMapper(); Set<Employee>?myObjects?=?mapper.readValue(EntityUtils.toString(entity),?new?TypeReference<Set<Employee>>(){});
對象 Employee 需要是響應(yīng)的準確表示
添加回答
舉報
0/150
提交
取消