亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 http 響應放入 Set

如何將 http 響應放入 Set

PIPIONE 2023-05-10 13:34:38
如何將 http 響應放入 Employee 類型的集合(Collection Set)中。我正在獲取數據作為 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;}響應應保存在 Set 中
查看完整描述

1 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

您可以使用庫 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>>(){});

對象 Employee 需要是響應的準確表示


查看完整回答
反對 回復 2023-05-10
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號