码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-11-21 13:48:40      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:array   hsv   sage   list   ima   返回   tco   tle   img   

更多精彩关注公众号

技术图片

原代码: 创建Dto给前端返值

    @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);
    }
    

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

标签:array   hsv   sage   list   ima   返回   tco   tle   img   

原文地址:https://www.cnblogs.com/mzdljgz/p/11905060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!