更多精彩关注公众号

controller通过map返回减少dto类的创建

不要把实体类对象直接返给前端 ,首先想到的是创建DTO,但是这样就造成大量的DTO,显得很臃肿,为了减少dto的数量,像一些比较少的参数避免创建不必要的DTO,通过本次优化达到业务的目的
(VO按数据库表创建,DTO用于接收或映射Http请求,在BO层将DTO赋值给各个VO)

本次仅仅是单表查询 传给前端数据 ,当然服务端接受数据也可以 用map接,思想都是一样的具体跟业务有关

适不适合 可以看看 这几个探讨:
@PostMapping({"/question/choice"}) public ResponseEntity findExercises(@RequestBody @NotEmpty(message = "{exercises.id.NotEmpty.message}") List<String> cmd5s) { List<ChoiceQuestion> contentMd5s = service.findAllByContentMd5In(cmd5s); List<ChoiceRtnDto> repetitions = contentMd5s .stream() .map(e -> new ChoiceRtnDto(e.getContentMd5(), e.getTitle(), e.getPid())) .collect(Collectors.toList()); return ResponseEntity.status(HttpStatus.ALREADY_REPORTED).body(repetitions); }

更改后:


@PostMapping({"/question/choice"})
    public ResponseEntity findExercises(@RequestBody @NotEmpty(message = "{exercises.id.NotEmpty.message}") List<String> md5List) {
        List<Map<String, String>> repetitiveQuestions = new ArrayList<>();
        service.findAllByContentMd5In(md5List).forEach(e -> {
            Map<String, String> map = new HashMap<>();
            map.put("md5", e.getContentMd5());
            map.put("name", e.getTitle());
            map.put("pid", e.getPid());
            repetitiveQuestions.add(map);
        });
        return ResponseEntity.status(HttpStatus.ALREADY_REPORTED).body(repetitiveQuestions);
    }
    

相关文章:

  • 2022-12-23
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2021-07-30
  • 2021-09-21
猜你喜欢
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2021-07-01
  • 2021-05-10
  • 2021-08-05
相关资源
相似解决方案