返回顶部
首页 > 资讯 > 后端开发 > Python >详解java封装返回结果与RestControllerAdvice注解
  • 264
分享到

详解java封装返回结果与RestControllerAdvice注解

2024-04-02 19:04:59 264人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录1.为什么要把结果封装成统一格式?2.统一异常接收1.为什么要把结果封装成统一格式? 异常和正常情况无法区分:异常情况返回数据为null,正常情况下查询结果也为null,返回给前

1.为什么要把结果封装成统一格式?

异常和正常情况无法区分:异常情况返回数据为null,正常情况下查询结果也为null,返回给前端无法区分

显示抛出异常信息:前端需要显示抛出的异常信息

@AllArgsConstructor
public enum ResultCode {
    CODE_200(200, "success"),
    CODE_1000(1000, "fail");
    @Getter
    private Integer code;
    @Getter
    private String msg;
}
@Data
public class Result<T> {
    private String msg;
    private Integer code;
    private T data;
    private Result(String msg, Integer code) {
        this.msg = msg;
        this.code = code;
    }
    private Result(ResultCode resultCode, String msg) {
        this(msg, resultCode.getCode());
    }
    private Result(ResultCode resultCode) {
        this(resultCode.getMsg(), resultCode.getCode());
    }
    public static <T>Result<T> result(ResultCode resultCode,T data) {
        Result result = new Result(resultCode);
        result.setData(data);
        return result;
    }
    public static <T>Result<T> fail(ResultCode resultCode,String message) {
        Result result = new Result(resultCode, message);
        result.setData(null);
        return result;
    }
    public static <T>Result<T> fail(T data) {
        Result result = new Result(ResultCode.CODE_1000);
        result.setData(data);
        return result;
    }
    public static <T>Result<T> success(T data) {
        Result result = new Result(ResultCode.CODE_200);
        result.setData(data);
        return result;
    }
}

不封装:

封装:

2.统一异常接收

为什么要用统一异常接收?

通常在service层抛异常,涉及到事务时不会进行try-catch,需要在controller里处理异常。即使能够进行try-catch的地方也需要统一返回格式,造成重复代码很多,可读性比较差。

如何实现统一的异常接收?

@Slf4j
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(Exception.class)
    public Result<?> handlerException(Exception exception) {
        log.info("Exception异常信息:" + exception.getMessage());
        return Result.fail(ResultCode.CODE_1000, exception.getMessage());
    }
    @ExceptionHandler(value = {MyException.class})
    public Result<?> handlerMyException(Exception exception) {
        log.info("MyException异常信息:" + exception.getMessage());
        return Result.fail(ResultCode.CODE_1000, exception.getMessage());
    }
}

抛出异常后,会进入到@RestControllerAdvice注解的方法,过滤出和抛出异常相同的class的方法,执行相应的方法。

protected ModelAndView doResolveHandlerMethodException(httpservletRequest request, HttpServletResponse response, @Nullable HandlerMethod handlerMethod, Exception exception) {
    //根据异常类型过滤方法
    ServletInvocableHandlerMethod exceptionHandlerMethod = this.getExceptionHandlerMethod(handlerMethod, exception);
    if (exceptionHandlerMethod == null) {
        return null;
    } else {
        if (this.argumentResolvers != null) {
            exceptionHandlerMethod.setHandlerMethodArgumentResolvers(this.argumentResolvers);
        }
        if (this.returnValueHandlers != null) {
            exceptionHandlerMethod.setHandlerMethodReturnValueHandlers(this.returnValueHandlers);
        }
        ServletWEBRequest webRequest = new ServletWebRequest(request, response);
        ModelAndViewContainer mavContainer = new ModelAndViewContainer();
        ArrayList<Throwable> exceptions = new ArrayList();
        try {
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("Using @ExceptionHandler " + exceptionHandlerMethod);
            }
            Throwable cause;
            for(Throwable exToExpose = exception; exToExpose != null; exToExpose = cause != exToExpose ? cause : null) {
                exceptions.add(exToExpose);
                cause = ((Throwable)exToExpose).getCause();
            }
            Object[] arguments = new Object[exceptions.size() + 1];
            exceptions.toArray(arguments);
            arguments[arguments.length - 1] = handlerMethod;
            //执行方法
            exceptionHandlerMethod.invokeAndHandle(webRequest, mavContainer, arguments);
        } catch (Throwable var13) {
            if (!exceptions.contains(var13) && this.logger.isWarnEnabled()) {
                this.logger.warn("Failure in @ExceptionHandler " + exceptionHandlerMethod, var13);
            }
            return null;
        }
        if (mavContainer.isRequestHandled()) {
            return new ModelAndView();
        } else {
            ModelMap model = mavContainer.getModel();
            HttpStatus status = mavContainer.getStatus();
            ModelAndView mav = new ModelAndView(mavContainer.getViewName(), model, status);
            mav.setViewName(mavContainer.getViewName());
            if (!mavContainer.isViewReference()) {
                mav.setView((View)mavContainer.getView());
            }
            if (model instanceof RedirectAttributes) {
                Map<String, ?> flashAttributes = ((RedirectAttributes)model).getFlashAttributes();
                RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
            }
            return mav;
        }
    }
}
protected ServletInvocableHandlerMethod getExceptionHandlerMethod(@Nullable HandlerMethod handlerMethod, Exception exception) {
    Class<?> handlerType = null;
    if (handlerMethod != null) {
        handlerType = handlerMethod.getBeanType();
        ExceptionHandlerMethodResolver resolver = (ExceptionHandlerMethodResolver)this.exceptionHandlerCache.get(handlerType);
        if (resolver == null) {
            resolver = new ExceptionHandlerMethodResolver(handlerType);
            this.exceptionHandlerCache.put(handlerType, resolver);
        }
        Method method = resolver.resolveMethod(exception);
        if (method != null) {
            return new ServletInvocableHandlerMethod(handlerMethod.getBean(), method, this.applicationContext);
        }
        if (Proxy.isProxyClass(handlerType)) {
            handlerType = aopUtils.getTargetClass(handlerMethod.getBean());
        }
    }
    //初始化的时候已经将beanType作为key,标注@ExceptionHandler的方法包装成resolve作为value放到exceptionHandlerAdviceCache中
    Iterator var9 = this.exceptionHandlerAdviceCache.entrySet().iterator();
    while(var9.hasNext()) {
        Map.Entry<ControllerAdviceBean, ExceptionHandlerMethodResolver> entry = (Map.Entry)var9.next();
        ControllerAdviceBean advice = (ControllerAdviceBean)entry.geTKEy();
        if (advice.isApplicableToBeanType(handlerType)) {
            ExceptionHandlerMethodResolver resolver = (ExceptionHandlerMethodResolver)entry.getValue();
            //根据class进行筛选
            Method method = resolver.resolveMethod(exception);
            if (method != null) {
                return new ServletInvocableHandlerMethod(advice.resolveBean(), method, this.applicationContext);
            }
        }
    }
    return null;
}

直接在service层抛出异常,controller中返回正常的结果,由标注了@RestControllerAdvice的类对异常进行处理

@RestController
@RequestMapping("/user")
public class UserController {
    @GetMapping("/test")
    public Result<Boolean> testGlobalException() {
        globalException.throwException();
        return Result.success(Boolean.TRUE);
    }
}

以上就是详解java封装返回结果与RestControllerAdvice注解的详细内容,更多关于java RestControllerAdvice 封装返回的资料请关注编程网其它相关文章!

--结束END--

本文标题: 详解java封装返回结果与RestControllerAdvice注解

本文链接: https://lsjlt.com/news/167640.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作