From c7414e18e71a4a35772c4d396b2d3f9cdfe47ad9 Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 12 Jan 2024 16:25:01 +0800 Subject: [PATCH] 1.uniform response status code for music component --- luoo_common/src/main/java/api/StatusCode.java | 12 +-- .../com/luoo/music/client/UserClient.java | 2 +- .../music/controller/ArticleController.java | 81 +++++++++---------- .../controller/BaseExceptionHandler.java | 13 ++- .../music/controller/ChannelController.java | 55 ++++++------- .../music/controller/ColumnController.java | 55 ++++++------- .../luoo/music/controller/SongController.java | 67 +++++++-------- .../luoo/user/controller/AdminController.java | 2 +- 8 files changed, 128 insertions(+), 159 deletions(-) diff --git a/luoo_common/src/main/java/api/StatusCode.java b/luoo_common/src/main/java/api/StatusCode.java index a424728..b8bd5ce 100644 --- a/luoo_common/src/main/java/api/StatusCode.java +++ b/luoo_common/src/main/java/api/StatusCode.java @@ -14,17 +14,13 @@ public enum StatusCode implements IErrorCode { USER_VERIFICATION_CODE_MISMATCH(10002, "验证码校验失败,请输入正确的验证码"), USER_NAME_OR_PASSWORD_FAILED(10003, "用户名或密码错误"), // music 模块错误码以20XXX,不足5位补0; - + MUSIC_COMMON_FAILED(20000, "歌曲模块失败"), // comment 模块错误码以30XXX,不足5位补0; - + COMMENT_COMMON_FAILED(30000, "评论模块失败"), // friend 模块错误码以40XXX,不足5位补0; - + FRIEND_COMMON_FAILED(40000, "交友模块失败"), // tag 模块错误码以50XXX,不足5位补0; - - // music 模块错误码以60XXX,不足5位补0; - - // manage 模块错误码以70XXX,不足5位补0; - MANAGE_COMMON_FAILED(70000, "管理模块失败"); + TAG_COMMON_FAILED(50000, "标签模块失败"); private int code; private String message; diff --git a/luoo_music/src/main/java/com/luoo/music/client/UserClient.java b/luoo_music/src/main/java/com/luoo/music/client/UserClient.java index 7727aa3..6a7099d 100644 --- a/luoo_music/src/main/java/com/luoo/music/client/UserClient.java +++ b/luoo_music/src/main/java/com/luoo/music/client/UserClient.java @@ -1,6 +1,6 @@ package com.luoo.music.client; -import entity.Result; +import api.Result; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; diff --git a/luoo_music/src/main/java/com/luoo/music/controller/ArticleController.java b/luoo_music/src/main/java/com/luoo/music/controller/ArticleController.java index cd9daa9..d315454 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/ArticleController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/ArticleController.java @@ -5,19 +5,13 @@ import java.util.Map; import com.luoo.music.client.UserClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - +import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Article; import com.luoo.music.service.ArticleService; -import entity.PageResult; -import entity.Result; -import entity.StatusCode; +import api.PageResult; +import api.Result; + /** * 控制器层 * @author Administrator @@ -34,33 +28,32 @@ public class ArticleController { @Autowired private UserClient userClient; - @RequestMapping(value = "/admin",method = RequestMethod.GET) - public Result findAdmin(){ - Result result =userClient.findAll(); - return result; + @GetMapping("/admin") + public Result findAdmin(){ + return userClient.findAll(); } - @RequestMapping(value = "/examine/{articleId}",method = RequestMethod.PUT) - public Result examine (@PathVariable String articleId){ + @PutMapping("/examine/{articleId}") + public Result examine (@PathVariable String articleId){ articleService.updateState(articleId); - return new Result(true,StatusCode.OK,"审核成功"); + return Result.success(); } - @RequestMapping(value = "/thumbup/{articleId}",method = RequestMethod.PUT) - public Result thumbup (@PathVariable String articleId){ + @PutMapping("/thumbup/{articleId}") + public Result thumbup (@PathVariable String articleId){ articleService.addThumbup(articleId); - return new Result(true,StatusCode.OK,"点赞成功"); + return Result.success(); } /** * 查询全部数据 * @return */ - @RequestMapping(method= RequestMethod.GET) - public Result findAll(){ - return new Result(true,StatusCode.OK,"查询成功",articleService.findAll()); + @GetMapping + public Result> findAll(){ + return Result.success(articleService.findAll()); } /** @@ -68,14 +61,14 @@ public class ArticleController { * @param id ID * @return */ - @RequestMapping(value="/{id}",method= RequestMethod.GET) - public Result findById(@PathVariable String id){ - return new Result(true,StatusCode.OK,"查询成功",articleService.findById(id)); + @GetMapping("/{id}") + public Result
findById(@PathVariable String id){ + return Result.success(articleService.findById(id)); } - @RequestMapping(value = "/findByVolid/{volid}",method = RequestMethod.GET) - public Result findByVolid(@PathVariable String volid){ - return new Result(true,StatusCode.OK,"查询成功",articleService.findByVolid(volid)); + @GetMapping("/findByVolid/{volid}") + public Result
findByVolid(@PathVariable String volid){ + return Result.success(articleService.findByVolid(volid)); } @@ -86,10 +79,10 @@ public class ArticleController { * @param size 页大小 * @return 分页结果 */ - @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST) - public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ + @PostMapping("/search/{page}/{size}") + public Result> findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ Page
pageList = articleService.findSearch(searchMap, page, size); - return new Result(true,StatusCode.OK,"查询成功", new PageResult
(pageList.getTotalElements(), pageList.getContent()) ); + return Result.success(new PageResult
(pageList.getTotalElements(), pageList.getContent()) ); } /** @@ -97,40 +90,40 @@ public class ArticleController { * @param searchMap * @return */ - @RequestMapping(value="/search",method = RequestMethod.POST) - public Result findSearch( @RequestBody Map searchMap){ - return new Result(true,StatusCode.OK,"查询成功",articleService.findSearch(searchMap)); + @PostMapping("/search") + public Result> findSearch( @RequestBody Map searchMap){ + return Result.success(articleService.findSearch(searchMap)); } /** * 增加 * @param article */ - @RequestMapping(method=RequestMethod.POST) - public Result add(@RequestBody Article article ){ + @PostMapping + public Result add(@RequestBody Article article ){ articleService.add(article); - return new Result(true,StatusCode.OK,"增加成功"); + return Result.success(); } /** * 修改 * @param article */ - @RequestMapping(value="/{id}",method= RequestMethod.PUT) - public Result update(@RequestBody Article article, @PathVariable String id ){ + @PutMapping("/{id}") + public Result update(@RequestBody Article article, @PathVariable String id ){ article.setId(id); articleService.update(article); - return new Result(true,StatusCode.OK,"修改成功"); + return Result.success(); } /** * 删除 * @param id */ - @RequestMapping(value="/{id}",method= RequestMethod.DELETE) - public Result delete(@PathVariable String id ){ + @DeleteMapping("/{id}") + public Result delete(@PathVariable String id ){ articleService.deleteById(id); - return new Result(true,StatusCode.OK,"删除成功"); + return Result.success(); } } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java index 24d1b63..5bfcce7 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/BaseExceptionHandler.java @@ -1,12 +1,9 @@ package com.luoo.music.controller; -import entity.Result; -import entity.StatusCode; +import api.Result; +import api.StatusCode; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletResponse; -import java.io.IOException; /** * 统一异常处理类 */ @@ -15,8 +12,8 @@ public class BaseExceptionHandler { @ExceptionHandler(value = Exception.class) @ResponseBody - public Result error(Exception e){ - e.printStackTrace(); - return new Result(false, StatusCode.ERROR, "执行出错"); + public Result error(Exception e){ + e.printStackTrace(); + return Result.failed(StatusCode.MUSIC_COMMON_FAILED); } } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/ChannelController.java b/luoo_music/src/main/java/com/luoo/music/controller/ChannelController.java index cf8c077..1483bea 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/ChannelController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/ChannelController.java @@ -4,19 +4,14 @@ import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Channel; import com.luoo.music.service.ChannelService; -import entity.PageResult; -import entity.Result; -import entity.StatusCode; +import api.PageResult; +import api.Result; + /** * 控制器层 * @author Administrator @@ -35,9 +30,9 @@ public class ChannelController { * 查询全部数据 * @return */ - @RequestMapping(method= RequestMethod.GET) - public Result findAll(){ - return new Result(true,StatusCode.OK,"查询成功",channelService.findAll()); + @GetMapping + public Result> findAll(){ + return Result.success(channelService.findAll()); } /** @@ -45,9 +40,9 @@ public class ChannelController { * @param id ID * @return */ - @RequestMapping(value="/{id}",method= RequestMethod.GET) - public Result findById(@PathVariable String id){ - return new Result(true,StatusCode.OK,"查询成功",channelService.findById(id)); + @GetMapping("/{id}") + public Result findById(@PathVariable String id){ + return Result.success(channelService.findById(id)); } @@ -58,10 +53,10 @@ public class ChannelController { * @param size 页大小 * @return 分页结果 */ - @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST) - public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ + @PostMapping("/search/{page}/{size}") + public Result> findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ Page pageList = channelService.findSearch(searchMap, page, size); - return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) ); + return Result.success(new PageResult(pageList.getTotalElements(), pageList.getContent()) ); } /** @@ -69,40 +64,40 @@ public class ChannelController { * @param searchMap * @return */ - @RequestMapping(value="/search",method = RequestMethod.POST) - public Result findSearch( @RequestBody Map searchMap){ - return new Result(true,StatusCode.OK,"查询成功",channelService.findSearch(searchMap)); + @PostMapping("/search") + public Result> findSearch( @RequestBody Map searchMap){ + return Result.success(channelService.findSearch(searchMap)); } /** * 增加 * @param channel */ - @RequestMapping(method=RequestMethod.POST) - public Result add(@RequestBody Channel channel ){ + @PostMapping + public Result add(@RequestBody Channel channel ){ channelService.add(channel); - return new Result(true,StatusCode.OK,"增加成功"); + return Result.success(); } /** * 修改 * @param channel */ - @RequestMapping(value="/{id}",method= RequestMethod.PUT) - public Result update(@RequestBody Channel channel, @PathVariable String id ){ + @PutMapping("/{id}") + public Result update(@RequestBody Channel channel, @PathVariable String id ){ channel.setId(id); channelService.update(channel); - return new Result(true,StatusCode.OK,"修改成功"); + return Result.success(); } /** * 删除 * @param id */ - @RequestMapping(value="/{id}",method= RequestMethod.DELETE) - public Result delete(@PathVariable String id ){ + @DeleteMapping("/{id}") + public Result delete(@PathVariable String id ){ channelService.deleteById(id); - return new Result(true,StatusCode.OK,"删除成功"); + return Result.success(); } } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/ColumnController.java b/luoo_music/src/main/java/com/luoo/music/controller/ColumnController.java index 02f510b..625fdea 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/ColumnController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/ColumnController.java @@ -4,19 +4,14 @@ import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; -import org.springframework.web.bind.annotation.CrossOrigin; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import com.luoo.music.pojo.Column; import com.luoo.music.service.ColumnService; -import entity.PageResult; -import entity.Result; -import entity.StatusCode; +import api.PageResult; +import api.Result; + /** * 控制器层 * @author Administrator @@ -35,9 +30,9 @@ public class ColumnController { * 查询全部数据 * @return */ - @RequestMapping(method= RequestMethod.GET) - public Result findAll(){ - return new Result(true,StatusCode.OK,"查询成功",columnService.findAll()); + @GetMapping + public Result> findAll(){ + return Result.success(columnService.findAll()); } /** @@ -45,9 +40,9 @@ public class ColumnController { * @param id ID * @return */ - @RequestMapping(value="/{id}",method= RequestMethod.GET) - public Result findById(@PathVariable String id){ - return new Result(true,StatusCode.OK,"查询成功",columnService.findById(id)); + @GetMapping("/{id}") + public Result findById(@PathVariable String id){ + return Result.success(columnService.findById(id)); } @@ -58,10 +53,10 @@ public class ColumnController { * @param size 页大小 * @return 分页结果 */ - @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST) - public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ + @PostMapping("/search/{page}/{size}") + public Result> findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ Page pageList = columnService.findSearch(searchMap, page, size); - return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) ); + return Result.success(new PageResult(pageList.getTotalElements(), pageList.getContent()) ); } /** @@ -69,40 +64,40 @@ public class ColumnController { * @param searchMap * @return */ - @RequestMapping(value="/search",method = RequestMethod.POST) - public Result findSearch( @RequestBody Map searchMap){ - return new Result(true,StatusCode.OK,"查询成功",columnService.findSearch(searchMap)); + @PostMapping("/search") + public Result> findSearch( @RequestBody Map searchMap){ + return Result.success(columnService.findSearch(searchMap)); } /** * 增加 * @param column */ - @RequestMapping(method=RequestMethod.POST) - public Result add(@RequestBody Column column ){ + @PostMapping + public Result add(@RequestBody Column column ){ columnService.add(column); - return new Result(true,StatusCode.OK,"增加成功"); + return Result.success(); } /** * 修改 * @param column */ - @RequestMapping(value="/{id}",method= RequestMethod.PUT) - public Result update(@RequestBody Column column, @PathVariable String id ){ + @PutMapping("/{id}") + public Result update(@RequestBody Column column, @PathVariable String id ){ column.setId(id); columnService.update(column); - return new Result(true,StatusCode.OK,"修改成功"); + return Result.success(); } /** * 删除 * @param id */ - @RequestMapping(value="/{id}",method= RequestMethod.DELETE) - public Result delete(@PathVariable String id ){ + @DeleteMapping("/{id}") + public Result delete(@PathVariable String id ){ columnService.deleteById(id); - return new Result(true,StatusCode.OK,"删除成功"); + return Result.success(); } } diff --git a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java index 95618b6..1d272ba 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/SongController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/SongController.java @@ -3,13 +3,11 @@ package com.luoo.music.controller; import com.luoo.music.pojo.Article; import com.luoo.music.pojo.Song; -import com.luoo.music.service.ArticleService; +import com.luoo.music.pojo.SongVO; import com.luoo.music.service.SongService; -import entity.PageResult; -import entity.Result; -import entity.StatusCode; +import api.PageResult; +import api.Result; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.json.JsonParser; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; @@ -26,23 +24,18 @@ public class SongController { @Autowired private SongService songService; - - - /** * 查询全部数据 * @return */ - @RequestMapping(method= RequestMethod.GET) - public Result findAll(){ - return new Result(true, StatusCode.OK,"查询成功",songService.findAll()); + @GetMapping + public Result> findAll(){ + return Result.success(songService.findAll()); } - - - @RequestMapping(value = "/findByVolid/{volid}",method = RequestMethod.GET) - public Result findByVolid(@PathVariable String volid){ - return new Result(true,StatusCode.OK,"查询成功",songService.findByVolid(volid)); + @GetMapping("/findByVolid/{volid}") + public Result> findByVolid(@PathVariable String volid){ + return Result.success(songService.findByVolid(volid)); } /** @@ -50,9 +43,9 @@ public class SongController { * @param id ID * @return */ - @RequestMapping(value="/{id}",method= RequestMethod.GET) - public Result findById(@PathVariable String id){ - return new Result(true,StatusCode.OK,"查询成功",songService.findById(id)); + @GetMapping("/{id}") + public Result findById(@PathVariable String id){ + return Result.success(songService.findById(id)); } @@ -63,15 +56,15 @@ public class SongController { * @param size 页大小 * @return 分页结果 */ - @RequestMapping(value="/search/{page}/{size}",method=RequestMethod.POST) - public Result findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ + @PostMapping("/search/{page}/{size}") + public Result> findSearch(@RequestBody Map searchMap , @PathVariable int page, @PathVariable int size){ Page pageList = songService.findSearch(searchMap, page, size); - return new Result(true,StatusCode.OK,"查询成功", new PageResult(pageList.getTotalElements(), pageList.getContent()) ); + return Result.success(new PageResult(pageList.getTotalElements(), pageList.getContent()) ); } - @RequestMapping(value = "/init",method = RequestMethod.POST) - public Result init(@RequestBody Map map) { + @PostMapping("/init") + public Result init(@RequestBody Map map) { List data = (List) map.get("data"); System.out.println(data.size()); Song song = new Song(); @@ -112,7 +105,7 @@ public class SongController { for (Article pojo : set) { // articleService.add(pojo); } - return new Result(true,StatusCode.OK,"init"); + return Result.success(); } /** @@ -120,9 +113,9 @@ public class SongController { * @param searchMap * @return */ - @RequestMapping(value="/search",method = RequestMethod.POST) - public Result findSearch( @RequestBody Map searchMap){ - return new Result(true,StatusCode.OK,"查询成功",songService.findSearch(searchMap)); + @PostMapping("/search") + public Result> findSearch( @RequestBody Map searchMap){ + return Result.success(songService.findSearch(searchMap)); } @@ -130,30 +123,30 @@ public class SongController { * 增加 * @param song */ - @RequestMapping(method=RequestMethod.POST) - public Result add(@RequestBody Song song){ + @PostMapping + public Result add(@RequestBody Song song){ songService.add(song); - return new Result(true,StatusCode.OK,"增加成功"); + return Result.success(); } /** * 修改 * @param song */ - @RequestMapping(value="/{id}",method= RequestMethod.PUT) - public Result update(@RequestBody Song song, @PathVariable String id ){ + @PutMapping("/{id}") + public Result update(@RequestBody Song song, @PathVariable String id ){ song.setId(id); songService.update(song); - return new Result(true,StatusCode.OK,"修改成功"); + return Result.success(); } /** * 删除 * @param id */ - @RequestMapping(value="/{id}",method= RequestMethod.DELETE) - public Result delete(@PathVariable String id ){ + @DeleteMapping("/{id}") + public Result delete(@PathVariable String id ){ songService.deleteById(id); - return new Result(true,StatusCode.OK,"删除成功"); + return Result.success(); } } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/AdminController.java b/luoo_user/src/main/java/com/luoo/user/controller/AdminController.java index 1bed98f..25ff1fe 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/AdminController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/AdminController.java @@ -67,7 +67,7 @@ public class AdminController { * 查询全部数据 * @return */ - @GetMapping("/list") + @GetMapping public Result> findAll(){ return Result.success(adminService.findAll()); }