簡體   English   中英

如何在 java spring 引導中對 json 數組的兩個值求和?

[英]How to sum value of two values of a json array in java spring boot?

My Api response(for the url http://localhost:8080/findTotSum/12-01-2020/12-02-2020) in JSON format is this

[ { "id": 121982, "expdate": null, "exptype": null, "expamt": 1000 }, { "id": 121984, "expdate": null, "exptype": null, "expamt": 54}]

我想對兩個expamt求和,結果應該是1054,如果可能的話,應該單獨給出總和的響應

首先,您應該根據您的業務邏輯為/findTotSum端點更新 API。 如果你想得到幾個值的總和,你應該選擇正確的返回類型。

其次,您需要為此類操作實現一個服務,在這種情況下用於求和操作,並使用它 controller class。

響應可以更改為:

Class newResponseWithSum { 
  private List<SomeEntity> list;
  private int sum;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM