使用来自 Rest API 的值返回键

我想从 Rest API 端点返回 JSON 作为带有值的键。例子:


  {

    "terminal 1":

       {"date":"2018-10-06T00:00:00.000+0000","volume":111,"count":1},

    "terminal 2":

       {"date":"2018-11-06T00:00:00.000+0000","volume":122,"count":1}

  }

如何添加密钥?我想应该是这样的:


List<String<List<TopTerminalsDTO>>>>

你能给我一些代码示例吗?


清理最终代码的最新尝试:


@GetMapping("/terminals")

public ResponseEntity<Map<Integer, List<TopTerminalsDTO>>> getTopTerminalsVolumes(

        @RequestParam(value = "start_date", required = true) String start_date,

        @RequestParam(value = "end_date", required = true) String end_date) {


        LocalDateTime start_datel = LocalDateTime.now(Clock.systemUTC());

        LocalDateTime end_datel = LocalDateTime.now(Clock.systemUTC());


        final List<PaymentTransactionsDailyFacts> list = dashboardRepository.top_daily_transactions(start_datel, end_datel);


        final Collector<PaymentTransactionsDailyFacts, List<TopTerminalsDTO>, List<TopTerminalsDTO>> terminalsCollector =

                 Collector.of(

                    ArrayList::new,

                    (terminals, p) -> terminals.add(mapper.toTopTerminalsDTO(p)),

                    (accumulator, terminals) -> {

                       accumulator.addAll(terminals);

                       return accumulator;

                    }

                 );


        final Map<Integer, List<TopTerminalsDTO>> final_map = 

                     list.stream()

                         .filter(p -> p.getTerminal_id() != null)

                         .collect(Collectors.groupingBy(p -> p.getTerminal_id(), terminalsCollector));


    return ResponseEntity.ok(final_map);

}


动漫人物
浏览 58回答 2
2回答

沧海一幻觉

在您的 JSON 之后,testDate()应该返回Map<String, TopTerminalsDTO>而不是List.Map<String, TopTerminalsDTO> result = newHashMap();for (int i = 0; i <= 10; i++) {&nbsp; &nbsp; TopTerminalsDTO ttDto = new TopTerminalsDTO();&nbsp; &nbsp; ttDto.setCount(ThreadLocalRandom.current().nextInt(20, 500 + 1));&nbsp; &nbsp; LocalDate localDate = LocalDate.now().minus(Period.ofDays((new Random().nextInt(365 * 70))));&nbsp; &nbsp; Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());&nbsp; &nbsp; ttDto.setDate(date);&nbsp; &nbsp; ttDto.setVolume(ThreadLocalRandom.current().nextInt(300, 5000 + 1));&nbsp; &nbsp; result.put("terminal "+i, ttDto)}return result;当然,将休息方法的响应类型更改为ResponseEntity<Map<String, TopTerminalsDTO>>

慕仙森

这就是 Javascript 的dictionary样子。在 Java 中,正确的表示是Map<String, TopTerminalDto>.假设您有一个有序列表,并且您想返回一个Map带有生成的键的列表terminal{index}。final List<TopTerminalDto> list = ...final Map<String, TopTerminalDto> map =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IntStream.range(0, list.size())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .boxed()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(toMap(i -> "terminal" + i, i -> list.get(i)));Spring 端点将变为:@GetMapping("terminals")Map<String, TopTerminalDto> getTopTerminalVolumes() { ... }在ResponseEntitySpring 中不是强制性的。记住尽可能通过Stream(s) 工作,以产生没有中间临时状态的结果。附加示例:final List<PaymentTransactionsDailyFacts> list =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dashboardRepository.top_daily_transactions(start_datel, end_datel);final Map<String, TopTerminalDto> map =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(toMap(p -> p.getTerminal(), this::toDto))// Conversion methodprivate TopTerminalDto toDto(final PaymentTransactionsDailyFacts p) {&nbsp; &nbsp; // Implement conversion to Dto}对于与终端关联的多个值:final Map<Integer, List<TopTerminalDto>> map =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(p -> p.getTerminal() != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(groupingBy(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p -> p.getTerminal(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Collector.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList::new,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (terminals, p) -> terminals.add(toDto(p)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (accumulator, terminals) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accumulator.addAll(terminals);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return accumulator;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;));您可以通过提取收集器来清理代码。final Collector<Integer, List<TopTerminalDto>, List<TopTerminalDto>> terminalsCollector =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Collector.of(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList::new,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (terminals, p) -> terminals.add(toDto(p)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (accumulator, terminals) -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;accumulator.addAll(terminals);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return accumulator;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)final Map<Integer, List<TopTerminalDto>> map =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;list.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.filter(p -> p.getTerminal() != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(groupingBy(p -> p.getTerminal(), terminalsCollector));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java