ArgumentResolverAdvice

ArgumentResolverAdvice允许用户在ArgumentResolver参数解析器解析参数的前后添加业务逻辑以及修改解析后的参数。

接口定义

public interface ArgumentResolverAdvice {

    /**
     * 在ArgumentResolver.resolve()之前被调用
     */
    void beforeResolve(AsyncRequest request, AsyncResponse response);

    /**
     * 在ArgumentResolver.resolve()之后被调用, 并使用此方法的返回值作为参数绑定到对应的Controller参数上
     */
    Object afterResolved(Object arg, AsyncRequest request, AsyncResponse response);
}

自定义ArgumentResolverAdvice

ArgumentResovler相同, ArgumentResolverAdvice自定应时同样需要实现ArgumentResolverAdviceAdapterArgumentResolverAdviceFactory接口

方式1:实现 ArgumentResolverAdviceAdapter

接口定义

public interface ArgumentResolverAdviceAdapter
        extends ArgumentResolverPredicate, ArgumentResolverAdvice, Ordered {

    @Override
    default void beforeResolve(AsyncRequest request, AsyncResponse response) {
    }

    @Override
    default Object afterResolved(Object arg, AsyncRequest request, AsyncResponse response) {
        return arg;
    }

    @Override
    default int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
}

方式2:实现ArgumentResolverAdviceFactory

接口定义

public interface ArgumentResolverAdviceFactory extends ArgumentResolverPredicate, Ordered {
    /**
     * 生成ArgumentResolverAdvice
     */
    ArgumentResolverAdvice createResolverAdvice(Param param, ArgumentResolver resolver);

    @Override
    default int getOrder() {
        return HIGHEST_PRECEDENCE;
    }
}

ArgumentResolverAdviceAdapter接口以及ArgumentResolverAdviceFactory接口与ArgumentResolver中的ArgumentResolverAdapter接口以及ArgumentResolverFactory接口的使用方式相同, 这里不过多赘述。