自定义注解+aop统一封装函数返回结果

自定义注解+aop统一封装函数返回结果

自定义注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Result {
}

@Target 说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。在Annotation类型的声明中使用了target可更加明晰其修饰的目标。
  作用:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
取值(ElementType)有:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明

结果集包装类

@Data
public class ResultInfo implements Serializable {
   //返回消息
    private String msg;
    //返回数据
    private Object data;
    //状态码
    private int code;
    //是否成功
    private boolean success;

    public ResultInfo(){}
    public ResultInfo(ResultCode resultCode){
        this(resultCode,(Object)null,resultCode.getMessage());
    }
    private ResultInfo(ResultCode resultCode, String msg) {
        this(resultCode, (Object)null, msg);
    }
	public ResultInfo(ValidCode validCode) {
		this.code = validCode.getCode();
		this.msg = validCode.getValidMsg();
	}
    public ResultInfo(ResultCode resultCode, Object data) {
        this(resultCode, data, resultCode.getMessage());
    }

    private ResultInfo(ResultCode resultCode, Object data, String msg) {
        this(resultCode.getCode(), data, msg);
    }
    private ResultInfo(int code, Object data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.success = ResultCode.SUCCESS.code == code;
    }
    public void setResultCode(ResultCode resultCode) {
    	this.code = resultCode.getCode();
    	this.msg = resultCode.getMessage();
    }
    public static boolean isSuccess(@Nullable ResultInfo result) {
        return (Boolean) Optional.ofNullable(result).map((x) -> {
            return ObjectUtils.nullSafeEquals(ResultCode.SUCCESS.code, x.code);
        }).orElse(Boolean.FALSE);
    }
    public static boolean isNotSuccess(@Nullable ResultInfo result) {
        return !isSuccess(result);
    }

    public static  ResultInfo data(Object data) {
        return data(data, "操作成功");
    }

    public static ResultInfo data(Object data, String msg) {
        return data(200, data, msg);
    }

    public static ResultInfo data(int code, Object data, String msg) {
        return new ResultInfo(code, data, data == null ? "无数据" : msg);
    }
    public static ResultInfo success(String msg) {
        return new ResultInfo(ResultCode.SUCCESS, msg);
    }

    public static ResultInfo success(ResultCode resultCode) {
        return new ResultInfo(resultCode);
    }

    public static ResultInfo success(ResultCode resultCode, String msg) {
        return new ResultInfo(resultCode, msg);
    }

    public static ResultInfo fail(String msg) {
        return new ResultInfo(ResultCode.FAILURE, msg);
    }

    public static ResultInfo fail(int code, String msg) {
        return new ResultInfo(code, (Object)null, msg);
    }

    public static ResultInfo fail(ResultCode resultCode) {
        return new ResultInfo(resultCode);
    }

    public static ResultInfo fail(ResultCode resultCode, String msg) {
        return new ResultInfo(resultCode, msg);
    }

    public static ResultInfo status(boolean flag) {
        return flag ? success("操作成功") : fail("操作失败");
    }

    @Override
    public String toString() {
        return "ResultInfo [code=" + this.getCode() + ", success=" + this.isSuccess() + ", data=" + this.getData() + ", msg=" + this.getMsg() + "]";
    }
}

aop切面类

@Aspect
@Component
public class ResultAspect {
	
	/**
	 * Logger标记的切面点
	 */
	@Pointcut("@annotation(com.ylsaas.core.annotation.Result)")
    public void logPointCut() {
    }
 
	/**
	 * 执行完controller目标方法后再进行数据封装
	 *
	 * @param point
	 * @return
	 * @throws Throwable
	 * @throws InterruptedException
	 */
	@Around("logPointCut()")
	public ResultInfo around(ProceedingJoinPoint point) throws Throwable, InterruptedException {
		Object[] args = point.getArgs();
		if (args != null && args.length > 0 && args[0] instanceof PageInfoParam) {
			PageInfoParam pageInfoParam = (PageInfoParam) args[0];
			PageHelper.startPage(pageInfoParam.getPageNum(), pageInfoParam.getPageSize());
			PageInfo<Object> pageInfoResult = new PageInfo<>((List<Object>) point.proceed());
			ResultInfo resultInfo = new ResultInfo(ResultCode.SUCCESS, pageInfoResult);
			return resultInfo;
		}
		return new ResultInfo(ResultCode.SUCCESS, point.proceed());
	}
}

此处使用的是环绕通知,可以根据需求使用不同通知。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值