【问题标题】:Mapping result of guava multimap to Response Modelguava multimap 到响应模型的映射结果
【发布时间】:2020-01-23 15:33:36
【问题描述】:

在我的 Web 服务应用程序中,以下模型用作 UI 的统计信息合同,其中对于给定日期,我需要提供有多少 acceptedrejected 等的摘要:

@Data
public class StatusResponse {
    private LocalDate processedDate;
    private int accepted;
    private int rejected;
    private int failed;
    private int other;
}

我的数据库查询返回的汇总结果如下:

date        status              groupStatus     count

2020-01-01  ACC                 Accepted        3
2020-01-22  RJCT                Rejected        47
2020-01-22  NM                  Other           1
2020-01-23  NIY                 Failed          55

我有一个枚举 Status:

public enum Status{

    ACCEPTED("Accepted"),
    REJECTED("Rejected"),
    OTHER("Other"),
    FAILED("Failed");

    private final String text;

    Status (final String text) {
        this.text = text;
    }
}

在我的存储库类中,我将状态摘要查询并检索到 Guava MultiMap 对象中,其中键是日期,值是状态的 EnumMap,它是计数:

public Multimap<LocalDate, EnumMap<Status, Integer>> statusSummary(final Request search) {
    return this.namedParameterJdbcTemplate.query(this.QUERY,
            new MapSqlParameterSource()
                    .addValue("processedDate", search.getProcessedDate()),
            rs -> {
                final Multimap<LocalDate, EnumMap<Status, Integer>> statusMap = ArrayListMultimap.create();
                while (rs.next()) {
                    final EnumMap<Status, Integer> groupedStatusCount = new EnumMap<>(Status.class);
                    groupedStatusCount.put(fromValue(rs.getString("groupStatus")), rs.getInt("count"));
                    statusMap.put(rs.getDate("date").toLocalDate(), groupedStatusCount);
                }
                return statusMap;
            }
    );
}

因此,键(已处理的日期)可以与值具有相同的状态。例如,地图可以包含:

key --> 2020-01-23
   val1 --> OTHER 23
   val2 --> ACCEPTED 2
   val3 --> OTHER 4

我的问题和我正在努力完成的事情:

如何将statusMap 的结果映射到StatusResponse 并提供每个日期的摘要。所以上面的StatusResponse 是这样的:

{
    "processedDate": [
        2020,
        1,
        23
    ],
    "accepted": 2,
    "rejected": 0,
    "failed": 0,
    "other": 27,
    "date": "2020-01-23T00:00:00"
}

我正在使用 Java 8,需要一些帮助,如何通过流操作和以函数式风格使用 groupBy 来实现这一点?

此外,我需要进一步分组,将今天之前的所有内容分组到今天,将今天之后的所有内容分组到今天 + 1。

谢谢

【问题讨论】:

    标签: java spring jdbc guava spring-jdbc


    【解决方案1】:

    您可以尝试以下方法。摘要可以存储在Map 中,并使用来自Jackson 的JsonAnyGetter 注释在根级别展开。这将从响应中删除汇总字段并将内部值放在根级别。

    @Data
    @Builder
    public class StatusResponse {
        private LocalDate processedDate;
    
        @JsonAnyGetter
        private Map<Status, Integer> summary;
    }
    

    现在,让我们将 MultiMap 转换为所需的格式

    List<StatusResponse> responseList = data.entries().stream()
      .map(entry -> {
        Map<Status, Integer> summary = entry.getValue().entries().stream()
          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b));
    
        return StatusResponse.builder()
          .processedDate(entry.getKey())
          .summary(summary)
          .build();
      }).collect(Collectors.toList());
    

    现在,我们需要将LocaleDate 转换为正确的格式。我们可以使用jackson-datatype-jsr310。要转换为 'yyyy-mm-dd' 之类的格式,我们只需将 JavaTimeModule 注册到 ObjectMapper。

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());
    

    输出会是这样的。

    [{
       "processedDate":"2020-01-23",
        "FAILED":3,
        "ACCEPTED":2
    }]
    

    注意:如果数据库返回所有状态,即使值为零,它也会包含在响应中。因此,要让所有状态字段都响应,只需让它们都成为 multimap 的一部分。

    您可以进一步探索自定义序列化程序以微调日期格式。 here

    【讨论】:

    • 每个消息合同,我不能在响应中包含 summary 标记,它需要包含 acceptedrejected 等的各个值,即使它们不是 db 返回的元素。您能否更新您的答案以包含所有字段?
    • @M06H 如果您让数据库返回所有状态,即使值为零,也会包含在内。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    相关资源
    最近更新 更多