controller通过map返回减少dto类的创建
生活随笔
收集整理的這篇文章主要介紹了
controller通过map返回减少dto类的创建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
更多精彩關注公眾號
不要把實體類對象直接返給前端 ,首先想到的是創建DTO,但是這樣就造成大量的DTO,顯得很臃腫,為了減少dto的數量,像一些比較少的參數避免創建不必要的DTO,通過本次優化達到業務的目的
(VO按數據庫表創建,DTO用于接收或映射Http請求,在BO層將DTO賦值給各個VO)
適不適合 可以看看 這個探討:
使用Map取代VO和DTO是否合理
Java 數據查詢,使用 Map 還是實體類封裝?
使用JavaBean比Map作為參數在性能,維護性方面更好
總結 重點就是代碼維護性(作者不要寫自己, 看誰不順眼寫誰🤣); 性能問題;
原代碼: 創建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类的创建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前后端JSON传值
- 下一篇: TeamCity部署项目(解决本次部署失