Solon v3.0.8

问题:全局修改控制器的返回值(或结果)?

</> markdown

全局的话可使用 RouterInterceptor 进行提交确认(即修改返回结果)。参考示例:

@Component
public class DemoRouterInterceptor implements RouterInterceptor {
    @Inject
    private TransService transService;

    @Override
    public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {
        chain.doIntercept(ctx, mainHandler);
    }

     /**
     * 提交结果( render 执行前调用)//不要做太复杂的事情
     */
    @Override
    public Object postResult(Context ctx, Object result) throws Throwable {
        //提示:此处只适合做结果类型转换
        if (result != null && !(result instanceof Throwable) && ctx.action() != null) {
            result = transService.transOneLoop(result, true);
        }

        return result;
    }
}

RouterInterceptor 更多的内容,可参考:《过滤器、路由拦截器、拦截器》