• 通用返回类型定义


    定义业务异常类:

    1. import lombok.AllArgsConstructor;
    2. import lombok.Data;
    3. @Data
    4. @AllArgsConstructor
    5. public class BusinessException extends RuntimeException{
    6. private String errorCode;
    7. private String errorMsg;
    8. }

    错误枚举类:

    1. public enum UnifiedResponseEnums {
    2. BAD_PARAM("1002","参数有错"),
    3. NOT_FOUND("1003","资源不存在"),
    4. NO_PERMISSION("1004","权限不足"),
    5. BAD_INPUT_PARAM("1005","入参有问题"),
    6. BAD_EMAIL_PARAM("1006","email入参有问题"),
    7. BAD_ID_PARAM("1007","id入参有问题"),
    8. BAD_IP("1008","非法IP"),
    9. INVALLD_TOKEN("1009","无效令牌"),
    10. TOO_MANY_PARAMS("1010","参数过载");
    11. private String errorCode;
    12. private String errorMsg;
    13. UnifiedResponseEnums(String errorCode, String errorMsg) {
    14. this.errorCode = errorCode;
    15. this.errorMsg = errorMsg;
    16. }
    17. public String getErrorCode() {
    18. return errorCode;
    19. }
    20. public void setErrorCode(String errorCode) {
    21. this.errorCode = errorCode;
    22. }
    23. public String getErrorMsg() {
    24. return errorMsg;
    25. }
    26. public void setErrorMsg(String errorMsg) {
    27. this.errorMsg = errorMsg;
    28. }
    29. }

    Controller 中使用:

    1. UnifiedResponseEnums enum1 = UnifiedResponseEnums.valueOf(UnifiedResponseEnums.class,"BAD_PARAM");
    2. throw new BusinessException(enum1.getErrorCode(), enum1.getErrorMsg());

    设置通用返回结果:

    1. public class JsonResultObject {
    2. private String Code;
    3. private String errorCode;
    4. private String message;
    5. private String errorMessage;
    6. private T data;
    7. public JsonResultObject(String code, String message, T data) {
    8. Code = code;
    9. this.message = message;
    10. this.data = data;
    11. }
    12. public JsonResultObject(String code, String errorCode, String message, String errorMessage, T data) {
    13. Code = code;
    14. this.errorCode = errorCode;
    15. this.message = message;
    16. this.errorMessage = errorMessage;
    17. this.data = data;
    18. }
    19. public JsonResultObject(String errorCode, String errorMessage) {
    20. this.errorCode = errorCode;
    21. this.errorMessage = errorMessage;
    22. }
    23. public String getCode() {
    24. return Code;
    25. }
    26. public void setCode(String code) {
    27. Code = code;
    28. }
    29. public String getErrorCode() {
    30. return errorCode;
    31. }
    32. public void setErrorCode(String errorCode) {
    33. this.errorCode = errorCode;
    34. }
    35. public String getMessage() {
    36. return message;
    37. }
    38. public void setMessage(String message) {
    39. this.message = message;
    40. }
    41. public String getErrorMessage() {
    42. return errorMessage;
    43. }
    44. public void setErrorMessage(String errorMessage) {
    45. this.errorMessage = errorMessage;
    46. }
    47. public T getData() {
    48. return data;
    49. }
    50. public void setData(T data) {
    51. this.data = data;
    52. }
    53. }

    错误返回定义:

    1. public class ErrorResult {
    2. private String errorMessage;
    3. private String errorCode;
    4. private T data;
    5. public ErrorResult(String errorCode,String errorMessage, T data) {
    6. this.errorMessage = errorMessage;
    7. this.errorCode = errorCode;
    8. this.data = data;
    9. }
    10. public String getErrorMessage() {
    11. return errorMessage;
    12. }
    13. public void setErrorMessage(String errorMessage) {
    14. this.errorMessage = errorMessage;
    15. }
    16. public String getErrorCode() {
    17. return errorCode;
    18. }
    19. public void setErrorCode(String errorCode) {
    20. this.errorCode = errorCode;
    21. }
    22. public T getData() {
    23. return data;
    24. }
    25. public void setData(T data) {
    26. this.data = data;
    27. }
    28. }

     

  • 相关阅读:
    【STM32】【HAL库】【实用制作】遥控关灯
    爬虫从入门到入牢
    U-boot(三):start.S
    第三章 使用管理门户监视IRIS - 共享内存堆使用情况
    建立完整对象模型实例(包括类、关联、属 性、服务),对象模型-类间关系及其表示
    【开源教程4】疯壳·开源编队无人机-OPENMV 脚本烧写
    计算机毕业设计springboot+vue基本微信小程序的汽车俱乐部系统
    Python Google内购服务端验证
    解压主播狂揽4000w+播放,快手美食赛道又添新风向?
    springboot-服务启动后访问只显示Initializing Spring DispatcherServlet ‘dispatcherServlet
  • 原文地址:https://blog.csdn.net/m0_62943934/article/details/134553618