guava multimap 到 Response Model 的映射结果
Mapping result of guava multimap to Response Model
在我的 Web 服务应用程序中,以下模型作为 UI 统计信息的合同,其中对于给定日期,我需要提供多少 accepted
、[=17] 的摘要=]等:
@Data
public class StatusResponse {
private LocalDate processedDate;
private int accepted;
private int rejected;
private int failed;
private int other;
}
我的数据库查询returns结果汇总如下:
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;
}
}
在我的存储库 class 中,我查询状态摘要并将其检索到 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;
}
);
}
因此,一个键(processedDate)可以具有与值相同的状态。例如,地图可以包含:
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。
谢谢
您可以试试下面的方法。摘要可以存储在 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
}]
注意:如果数据库 returns 所有状态,即使值为零,它也会包含在响应中。因此,要让所有状态字段都响应,只需让它们都成为 multimap 的一部分。
您可以进一步探索自定义序列化程序以微调日期格式。
在我的 Web 服务应用程序中,以下模型作为 UI 统计信息的合同,其中对于给定日期,我需要提供多少 accepted
、[=17] 的摘要=]等:
@Data
public class StatusResponse {
private LocalDate processedDate;
private int accepted;
private int rejected;
private int failed;
private int other;
}
我的数据库查询returns结果汇总如下:
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;
}
}
在我的存储库 class 中,我查询状态摘要并将其检索到 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;
}
);
}
因此,一个键(processedDate)可以具有与值相同的状态。例如,地图可以包含:
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。
谢谢
您可以试试下面的方法。摘要可以存储在 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
}]
注意:如果数据库 returns 所有状态,即使值为零,它也会包含在响应中。因此,要让所有状态字段都响应,只需让它们都成为 multimap 的一部分。
您可以进一步探索自定义序列化程序以微调日期格式。