• java-spring返回类


    状态码类:

    1. public enum ServiceCode {
    2. OK(20000),
    3. ERR_BAD_REQUEST(40000),//错误请求
    4. ERR_NOT_FOUND(40400),//没有发现
    5. ERR_UNAUTHORIZED(40100),//未经授权
    6. ERR_UNAUTHORIZED_DISABLED(40110),//未经授权禁止
    7. ERR_FORBIDDEN(40300),//被禁止的
    8. ERR_CONFLICT(40900),//冲突
    9. ERR_INSERT(50000),//插入异常
    10. ERR_DELETE(50100),//删除异常
    11. ERR_UPDATE(50200),//更新异常
    12. ERR_SELECT(50300),//搜索异常
    13. ERR_JWT_EXPIRED(60000),//jwt过期
    14. ERR_JWT_MALFORMED(60100),//jwt格式不对
    15. ERR_JWT_SIGNATURE(60200),//jwl签名错误
    16. ERR_UNKNOWN(99999);//未知错误
    17. private Integer value;
    18. ServiceCode(Integer value) {
    19. this.value = value;
    20. }
    21. public Integer getValue() {
    22. return value;
    23. }
    24. }

    返回类:

    1. /**
    2. * 统一的响应结果类型
    3. */
    4. @Data //依赖导入lombok
    5. public class JsonResult implements Serializable {
    6. /**
    7. * 操作结果的状态码
    8. */
    9. @ApiModelProperty("业务状态码")
    10. private Integer state;
    11. /**
    12. * 操作“失败”时响应的提示文本
    13. */
    14. @ApiModelProperty("消息")
    15. private String message;
    16. /**
    17. * 操作“成功”时响应的数据
    18. */
    19. @ApiModelProperty("数据")
    20. private T data; // T=Type, E=Element, K=Key, V=Value。泛型
    21. public static JsonResult ok() {
    22. return ok(null);
    23. }
    24. public static JsonResult ok(T data) {
    25. JsonResult jsonResult = new JsonResult();
    26. jsonResult.setState(ServiceCode.OK.getValue());
    27. jsonResult.setData(data);
    28. return jsonResult;
    29. }
    30. public static JsonResult fail(ServiceException e) {
    31. return fail(e.getServiceCode(), e.getMessage());
    32. }
    33. public static JsonResult fail(ServiceCode serviceCode, String message) {
    34. JsonResult jsonResult = new JsonResult();
    35. jsonResult.setState(serviceCode.getValue());
    36. jsonResult.setMessage(message);
    37. return jsonResult;
    38. }
    39. }
    40. public static JsonResult fail(ServiceException e) {
    41. return fail(e.getServiceCode(), e.getMessage());
    42. }
    43. public static JsonResult fail(ServiceCode serviceCode, String message) {
    44. JsonResult jsonResult = new JsonResult();
    45. jsonResult.setState(serviceCode.getValue());
    46. jsonResult.setMessage(message);
    47. return jsonResult;
    48. }
    49. }

    (没有找到数据、不符合业务、查询失败等均抛出异常类,相当于if ···else···中的else情况)异常类:

    1. /**
    2. * 业务异常
    3. */
    4. public class ServiceException extends RuntimeException {
    5. private ServiceCode serviceCode;
    6. public ServiceException(ServiceCode serviceCode, String message) {
    7. super(message);
    8. this.serviceCode = serviceCode;
    9. }
    10. public ServiceCode getServiceCode() {
    11. return serviceCode;
    12. }
    13. }

    使用:

    1. //成功情况,不带数据,只返回2000状态码
    2. public JsonResult 方法名(参数(可选))
    3. return JsonResult.ok();
    4. }
    5. //成功情况,带数据.T表示需要返回的数据类型,如:对象、list、string·····
    6. public JsonResult 方法名(参数(可选))
    7. return JsonResult.ok(T)
    8. }
    9. //错误情况,ServiceCode.状态码。message为字符串
    10. JsonResult jsonResult = JsonResult.fail(ServiceCode.状态码, message);
    11. return jsonResult;
    12. //或者抛异常
    13. throw new ServiceException(ServiceCode.状态码, message);

  • 相关阅读:
    【hexo博客配置】hexo icarus主题配置
    结构体 2.招聘
    MES管理系统中的质量管理活动是什么
    判断两个DataFrame和array的列(Series)是否相同
    信创平台:查询CPU,内存等命令
    阈值同态加密在隐私计算中的应用:解读
    超强高温天气来袭,“幕后推手”是谁
    live555交叉编译(ubuntu+arm平台)
    AVM 环视拼接方法介绍
    利用BACnet分布式IO控制器优化Niagara楼宇自动化系统
  • 原文地址:https://blog.csdn.net/weixin_43689176/article/details/132794035