From e2cd163840078223e0c20154f6e08fc51274f3e3 Mon Sep 17 00:00:00 2001 From: Gary Date: Fri, 12 Jan 2024 15:52:31 +0800 Subject: [PATCH] 1.uniform response status code for user component --- luoo_common/src/main/java/api/IErrorCode.java | 11 + luoo_common/src/main/java/api/PageResult.java | 31 +++ luoo_common/src/main/java/api/Result.java | 123 ++++++++++++ luoo_common/src/main/java/api/StatusCode.java | 43 ++++ .../main/java/swagger/BaseSwaggerConfig.java | 19 +- .../luoo/user/controller/AdminController.java | 79 ++++---- .../user/controller/BaseExceptionHandler.java | 13 +- .../luoo/user/controller/UserController.java | 188 +++++++++--------- 8 files changed, 356 insertions(+), 151 deletions(-) create mode 100644 luoo_common/src/main/java/api/IErrorCode.java create mode 100644 luoo_common/src/main/java/api/PageResult.java create mode 100644 luoo_common/src/main/java/api/Result.java create mode 100644 luoo_common/src/main/java/api/StatusCode.java diff --git a/luoo_common/src/main/java/api/IErrorCode.java b/luoo_common/src/main/java/api/IErrorCode.java new file mode 100644 index 0000000..d0bcc58 --- /dev/null +++ b/luoo_common/src/main/java/api/IErrorCode.java @@ -0,0 +1,11 @@ +package api; + +/** + * 封装API的错误码 + * Created by Gary on 2024/1/12. + */ +public interface IErrorCode { + int getCode(); + + String getMessage(); +} diff --git a/luoo_common/src/main/java/api/PageResult.java b/luoo_common/src/main/java/api/PageResult.java new file mode 100644 index 0000000..99248c9 --- /dev/null +++ b/luoo_common/src/main/java/api/PageResult.java @@ -0,0 +1,31 @@ +package api; + +import java.util.List; + +public class PageResult { + + private Long total; + private List rows; + + public PageResult(Long total, List rows) { + super(); + this.total = total; + this.rows = rows; + } + + public Long getTotal() { + return total; + } + public void setTotal(Long total) { + this.total = total; + } + public List getRows() { + return rows; + } + public void setRows(List rows) { + this.rows = rows; + } + + + +} diff --git a/luoo_common/src/main/java/api/Result.java b/luoo_common/src/main/java/api/Result.java new file mode 100644 index 0000000..c9d4d69 --- /dev/null +++ b/luoo_common/src/main/java/api/Result.java @@ -0,0 +1,123 @@ +package api; + +public class Result { + private long code; + private String message; + private T data; + + protected Result() { + } + + protected Result(long code, String message, T data) { + this.code = code; + this.message = message; + this.data = data; + } + + public static Result success() { + return new Result(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), null); + } + /** + * 成功返回结果 + * + * @param data 获取的数据 + */ + public static Result success(T data) { + return new Result(StatusCode.SUCCESS.getCode(), StatusCode.SUCCESS.getMessage(), data); + } + + /** + * 成功返回结果 + * + * @param data 获取的数据 + * @param message 提示信息 + */ + public static Result success(T data, String message) { + return new Result(StatusCode.SUCCESS.getCode(), message, data); + } + + /** + * 失败返回结果 + * @param errorCode 错误码 + */ + public static Result failed(IErrorCode errorCode) { + return new Result(errorCode.getCode(), errorCode.getMessage(), null); + } + + /** + * 失败返回结果 + * @param errorCode 错误码 + * @param message 错误信息 + */ + public static Result failed(IErrorCode errorCode,String message) { + return new Result(errorCode.getCode(), message, null); + } + + /** + * 失败返回结果 + * @param message 提示信息 + */ + public static Result failed(String message) { + return new Result(StatusCode.FAILED.getCode(), message, null); + } + + /** + * 失败返回结果 + */ + public static Result failed() { + return failed(StatusCode.FAILED); + } + + /** + * 参数验证失败返回结果 + */ + public static Result validateFailed() { + return failed(StatusCode.VALIDATE_FAILED); + } + + /** + * 参数验证失败返回结果 + * @param message 提示信息 + */ + public static Result validateFailed(String message) { + return new Result(StatusCode.VALIDATE_FAILED.getCode(), message, null); + } + + /** + * 未登录返回结果 + */ + public static Result unauthorized(T data) { + return new Result(StatusCode.UNAUTHORIZED.getCode(), StatusCode.UNAUTHORIZED.getMessage(), data); + } + + /** + * 未授权返回结果 + */ + public static Result forbidden(T data) { + return new Result(StatusCode.FORBIDDEN.getCode(), StatusCode.FORBIDDEN.getMessage(), data); + } + + public long getCode() { + return code; + } + + public void setCode(long code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public T getData() { + return data; + } + + public void setData(T data) { + this.data = data; + } +} diff --git a/luoo_common/src/main/java/api/StatusCode.java b/luoo_common/src/main/java/api/StatusCode.java new file mode 100644 index 0000000..a424728 --- /dev/null +++ b/luoo_common/src/main/java/api/StatusCode.java @@ -0,0 +1,43 @@ +package api; + +public enum StatusCode implements IErrorCode { + //通用状态码 + SUCCESS(200, "操作成功"), + FAILED(500, "操作失败"), + VALIDATE_FAILED(404, "参数检验失败"), + UNAUTHORIZED(401, "暂未登录或token已经过期"), + FORBIDDEN(403, "没有相关权限"), + + //user 模块错误码以10XXX,不足5位补0; + USER_COMMON_FAILED(10000, "用户模块失败"), + USER_VERIFICATION_CODE_EXPIRED(10001, "验证码过期,请重新获取"), + USER_VERIFICATION_CODE_MISMATCH(10002, "验证码校验失败,请输入正确的验证码"), + USER_NAME_OR_PASSWORD_FAILED(10003, "用户名或密码错误"), + // music 模块错误码以20XXX,不足5位补0; + + // comment 模块错误码以30XXX,不足5位补0; + + // friend 模块错误码以40XXX,不足5位补0; + + // tag 模块错误码以50XXX,不足5位补0; + + // music 模块错误码以60XXX,不足5位补0; + + // manage 模块错误码以70XXX,不足5位补0; + MANAGE_COMMON_FAILED(70000, "管理模块失败"); + private int code; + private String message; + + private StatusCode(int code, String message) { + this.code = code; + this.message = message; + } + + public int getCode() { + return code; + } + + public String getMessage() { + return message; + } +} diff --git a/luoo_common/src/main/java/swagger/BaseSwaggerConfig.java b/luoo_common/src/main/java/swagger/BaseSwaggerConfig.java index 5d1e099..8849dba 100644 --- a/luoo_common/src/main/java/swagger/BaseSwaggerConfig.java +++ b/luoo_common/src/main/java/swagger/BaseSwaggerConfig.java @@ -3,6 +3,8 @@ package swagger; import org.springframework.context.annotation.Bean; import org.springframework.core.annotation.Order; import org.springframework.web.bind.annotation.RequestMethod; + +import api.StatusCode; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; @@ -12,6 +14,7 @@ import springfox.documentation.service.*; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public abstract class BaseSwaggerConfig { @@ -19,12 +22,16 @@ public abstract class BaseSwaggerConfig { @Bean @Order(value = 1) public Docket createRestApi() { - List responseMessageList =new ArrayList<>(); - responseMessageList.add(new ResponseMessageBuilder().code(200).message("成功").responseModel(new ModelRef("成功")).build()); - responseMessageList.add(new ResponseMessageBuilder().code(20001).message("失败").responseModel(new ModelRef("失败")).build()); - responseMessageList.add(new ResponseMessageBuilder().code(20002).message("用户名或密码错误").responseModel(new ModelRef("用户名或密码错误")).build()); - responseMessageList.add(new ResponseMessageBuilder().code(20003).message("无权访问").responseModel(new ModelRef("无权访问")).build()); - responseMessageList.add(new ResponseMessageBuilder().code(20009).message("重复提交").responseModel(new ModelRef("重复提交")).build()); + + //添加全局响应状态码 + List responseMessageList = new ArrayList<>(); + Arrays.stream(StatusCode.values()).forEach(errorEnums -> { + responseMessageList.add( + new ResponseMessageBuilder().code(errorEnums.getCode()).message(errorEnums.getMessage()).responseModel( + new ModelRef(errorEnums.getMessage())).build() + ); + }); + SwaggerProperties swaggerProperties = swaggerProperties(); return new Docket(DocumentationType.SWAGGER_2) .globalResponseMessage(RequestMethod.GET, responseMessageList) 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 4fbc88f..1bed98f 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 @@ -3,23 +3,16 @@ import java.util.*; 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.user.pojo.Admin; import com.luoo.user.service.AdminService; -import entity.PageResult; -import entity.Result; -import entity.StatusCode; +import api.PageResult; +import api.Result; +import api.StatusCode; import util.JwtUtil; - /** * 控制器层 * @author Administrator @@ -37,37 +30,36 @@ public class AdminController { private JwtUtil jwtUtil; - @RequestMapping(value = "/info",method = RequestMethod.GET) - public Result info(){ + @GetMapping("/info") + public Result info(){ Map map = new HashMap(); map.put("name","admin"); List list =new ArrayList<>(); list.add("admin"); map.put("roles",list); map.put("avatar",""); - return new Result(true,StatusCode.OK,"查询成功",map); + return Result.success(map); } - @RequestMapping(value = "/logout",method = RequestMethod.POST) - public Result logout(){ - - return new Result(true,StatusCode.OK,"登出成功"); + @PostMapping("/logout") + public Result logout(){ + return Result.success(); } - @RequestMapping(value = "/login",method = RequestMethod.POST) + @PostMapping("/login") public Result login(@RequestBody Map loginMap) { Admin admin = adminService.findByLoginnameAndPassword(loginMap.get("username"),loginMap.get("password")); if (admin !=null) { //生成token String token = jwtUtil.createJWT(admin.getId(),admin.getLoginname(),"admin"); - Map map = new HashMap(); + Map map = new HashMap<>(); map.put("token",token); map.put("roles","admin"); map.put("name",admin.getLoginname()); - return new Result(true,StatusCode.OK,"登录成功",map); + return Result.success(map); } else { - return new Result(false,StatusCode.LOGINERROR,"用户名或密码错误"); + return Result.failed(StatusCode.USER_NAME_OR_PASSWORD_FAILED); } } @@ -75,9 +67,9 @@ public class AdminController { * 查询全部数据 * @return */ - @RequestMapping(method= RequestMethod.GET) - public Result findAll(){ - return new Result(true,StatusCode.OK,"查询成功",adminService.findAll()); + @GetMapping("/list") + public Result> findAll(){ + return Result.success(adminService.findAll()); } /** @@ -85,9 +77,9 @@ public class AdminController { * @param id ID * @return */ - @RequestMapping(value="/{id}",method= RequestMethod.GET) - public Result findById(@PathVariable String id){ - return new Result(true,StatusCode.OK,"查询成功",adminService.findById(id)); + @GetMapping("/{id}") + public Result findById(@PathVariable String id){ + return Result.success(adminService.findById(id)); } @@ -98,10 +90,10 @@ public class AdminController { * @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 = adminService.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()) ); } /** @@ -109,41 +101,40 @@ public class AdminController { * @param searchMap * @return */ - @RequestMapping(value="/search",method = RequestMethod.POST) - public Result findSearch( @RequestBody Map searchMap){ - return new Result(true,StatusCode.OK,"查询成功",adminService.findSearch(searchMap)); + @PostMapping("/search") + public Result> findSearch( @RequestBody Map searchMap){ + return Result.success(adminService.findSearch(searchMap)); } /** * 增加 * @param admin */ - @RequestMapping(method=RequestMethod.POST) - public Result add(@RequestBody Admin admin ){ + @PostMapping + public Result add(@RequestBody Admin admin ){ adminService.add(admin); - return new Result(true,StatusCode.OK,"增加成功"); + return Result.success(); } /** * 修改 * @param admin */ - @RequestMapping(value="/{id}",method= RequestMethod.PUT) - public Result update(@RequestBody Admin admin, @PathVariable String id ){ + @PutMapping("/{id}") + public Result update(@RequestBody Admin admin, @PathVariable String id ){ admin.setId(id); adminService.update(admin); - 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 ){ adminService.deleteById(id); - return new Result(true,StatusCode.OK,"删除成功"); + return Result.success(); } } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java b/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java index 2db9396..346bc17 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/BaseExceptionHandler.java @@ -1,12 +1,9 @@ package com.luoo.user.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.USER_COMMON_FAILED); } } diff --git a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java index 9d29427..1c98ed5 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/UserController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/UserController.java @@ -1,5 +1,5 @@ package com.luoo.user.controller; -import java.util.Date; + import java.util.HashMap; import java.util.List; import java.util.Map; @@ -7,21 +7,14 @@ import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.redis.core.RedisTemplate; -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.user.dto.response.UserRespDTO; import com.luoo.user.pojo.User; import com.luoo.user.service.UserService; -import entity.PageResult; -import entity.Result; -import entity.ResultVO; -import entity.StatusCode; +import api.PageResult; +import api.Result; +import api.StatusCode; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import util.IdWorker; @@ -29,13 +22,14 @@ import util.JwtUtil; /** * 控制器层 + * * @author Administrator * */ @RestController @CrossOrigin @RequestMapping("/user") -@Api(tags="UserController") +@Api(tags = "UserController") public class UserController { @Autowired @@ -52,154 +46,162 @@ public class UserController { /** * 更新好友粉丝数和用户关注数 + * * @param * @return */ - @RequestMapping(value = "/{userid}/{friendid}/{x}",method = RequestMethod.PUT) - public void updatefanscountandfollowcount(@PathVariable String userid,@PathVariable String friendid,@PathVariable int x){ - userService.updatefanscountandfollowcount(x,userid,friendid); + @PutMapping("/{userid}/{friendid}/{x}") + public void updatefanscountandfollowcount(@PathVariable String userid, @PathVariable String friendid, + @PathVariable int x) { + userService.updatefanscountandfollowcount(x, userid, friendid); } - @RequestMapping(value = "/login",method = RequestMethod.POST) - public Result login(@RequestBody User user){ - - user = userService.login(user.getMobile(),user.getPassword()); + @PostMapping("/login") + @Deprecated + public Result login(@RequestBody User user) { + user = userService.login(user.getMobile(), user.getPassword()); if (user == null) { - return new Result(false,StatusCode.LOGINERROR,"登录失败"); + // before there is a 'login error, intend to remvoe this interface' + return Result.failed(); } - String token = jwtUtil.createJWT(user.getId(),user.getMobile(),"user"); + String token = jwtUtil.createJWT(user.getId(), user.getMobile(), "user"); Map map = new HashMap(); - map.put("userid",user.getId()); - map.put("mobile",user.getMobile()); - map.put("token",token); - map.put("name",user.getLoginname()); - map.put("avatar",user.getAvatar()); - map.put("roles","user"); - return new Result(true,StatusCode.OK,"登录成功",map); + map.put("userid", user.getId()); + map.put("mobile", user.getMobile()); + map.put("token", token); + map.put("name", user.getLoginname()); + map.put("avatar", user.getAvatar()); + map.put("roles", "user"); + return Result.success(map); } - @ApiOperation(value="2.登录/注册后返回token") - @RequestMapping(value = "/appLogin/{mobile}/{checkcode}",method = RequestMethod.POST) - public ResultVO appLogin(@PathVariable String mobile,@PathVariable String checkcode){ + @ApiOperation(value = "2.登录/注册后返回token") + @PostMapping("/appLogin/{mobile}/{checkcode}") + public Result appLogin(@PathVariable String mobile, @PathVariable String checkcode) { // 得到缓存中的验证码 - String checkcodeRedis = (String)redisTemplate.opsForValue().get("checkcode_"+mobile); - if (null==checkcodeRedis||checkcodeRedis.isEmpty()){ - return new ResultVO(false,StatusCode.ERROR,"请先获取手机验证码"); + String checkcodeRedis = (String) redisTemplate.opsForValue().get("checkcode_" + mobile); + if (null == checkcodeRedis || checkcodeRedis.isEmpty()) { + return Result.failed(StatusCode.USER_VERIFICATION_CODE_EXPIRED); } if (!checkcodeRedis.equals(checkcode)) { - return new ResultVO(false,StatusCode.ERROR,"请输入正确的验证码"); + return Result.failed(StatusCode.USER_VERIFICATION_CODE_MISMATCH); } - UserRespDTO userVO=userService.loginOrRegister(mobile); - return ResultVO.success(userVO); + UserRespDTO userRespDTO = userService.loginOrRegister(mobile); + return Result.success(userRespDTO); } - - @ApiOperation(value="3.游客登录返回token",notes="token中的subject和roles均为tourist") - @RequestMapping(value = "/touristLogin",method = RequestMethod.GET) - public ResultVO touristLogin(){ - UserRespDTO userVO=userService.touristLogin(); - return ResultVO.success(userVO); + + @ApiOperation(value = "3.游客登录返回token", notes = "token中的subject和roles均为tourist") + @GetMapping("/touristLogin") + public Result touristLogin() { + UserRespDTO userRespDTO = userService.touristLogin(); + return Result.success(userRespDTO); } /** * 发送短信验证码 */ - @ApiOperation(value="1.发送短信验证码") - @RequestMapping(value = "/sendsms/{mobile}",method = RequestMethod.POST) - public ResultVO sendSms(@PathVariable String mobile) { - + @ApiOperation(value = "1.发送短信验证码") + @PostMapping("/sendsms/{mobile}") + public Result sendSms(@PathVariable String mobile) { userService.sendSms(mobile); - return ResultVO.success("发送成功"); + return Result.success(); } - @RequestMapping(value = "/register/{code}",method = RequestMethod.POST) - public Result regist(@PathVariable String code,@RequestBody User user){ + @PostMapping("/register/{code}") + public Result regist(@PathVariable String code, @RequestBody User user) { // 得到缓存中的验证码 - String checkcodeRedis = (String) redisTemplate.opsForValue().get("checkcode_"+user.getMobile()); - if (checkcodeRedis.isEmpty()){ - //单独定义一个请重新获取验证码的code? 2024.01.12 by he,changgeng - return new Result(false,StatusCode.ERROR,"请先获取手机验证码"); + String checkcodeRedis = (String) redisTemplate.opsForValue().get("checkcode_" + user.getMobile()); + if (checkcodeRedis.isEmpty()) { + return Result.failed(StatusCode.USER_VERIFICATION_CODE_EXPIRED); } if (!checkcodeRedis.equals(code)) { - return new Result(false,StatusCode.ERROR,"请输入正确的验证码"); + return Result.failed(StatusCode.USER_VERIFICATION_CODE_MISMATCH); } - userService.add(user); - return new Result(true,StatusCode.OK,"注册成功"); + return Result.success(); } + /** * 查询全部数据 + * * @return */ - @RequestMapping(method= RequestMethod.GET) - public Result findAll(){ - return new Result(true,StatusCode.OK,"查询成功",userService.findAll()); + @GetMapping + public Result> findAll() { + return Result.success(userService.findAll()); } - + /** * 根据ID查询 + * * @param id ID * @return */ - @RequestMapping(value="/{id}",method= RequestMethod.GET) - public Result findById(@PathVariable String id){ - return new Result(true,StatusCode.OK,"查询成功",userService.findById(id)); + @GetMapping("/{id}") + public Result findById(@PathVariable String id) { + return Result.success(userService.findById(id)); } - /** * 分页+多条件查询 + * * @param searchMap 查询条件封装 - * @param page 页码 - * @param size 页大小 + * @param page 页码 + * @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 = userService.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())); } /** - * 根据条件查询 - * @param searchMap - * @return - */ - @RequestMapping(value="/search",method = RequestMethod.POST) - public Result findSearch( @RequestBody Map searchMap){ - return new Result(true,StatusCode.OK,"查询成功",userService.findSearch(searchMap)); - } - + * 根据条件查询 + * + * @param searchMap + * @return + */ + @PostMapping("/search") + public Result> findSearch(@RequestBody Map searchMap) { + return Result.success(userService.findSearch(searchMap)); + } + /** * 增加 + * * @param user */ - @RequestMapping(method=RequestMethod.POST) - public Result add(@RequestBody User user ){ + @PostMapping + public Result add(@RequestBody User user) { userService.add(user); - return new Result(true,StatusCode.OK,"增加成功"); + return Result.success(); } - + /** * 修改 + * * @param user */ - @RequestMapping(value="/{id}",method= RequestMethod.PUT) - public Result update(@RequestBody User user, @PathVariable String id ){ + @PutMapping("/{id}") + public Result update(@RequestBody User user, @PathVariable String id) { user.setId(id); - userService.update(user); - return new Result(true,StatusCode.OK,"修改成功"); + userService.update(user); + 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) { userService.deleteById(id); - return new Result(true,StatusCode.OK,"删除成功"); + return Result.success(); } - + }