@RestControllerAdvice注解作用和@ExceptionHandler全局异常配置

目录

@ExceptionHandler 全局异常配置

@ModelAttribute

@InitBinder 全局请求方法前置处理(可以不看)


@RestControllerAdvice没有单用的。一般都是配合这三个注解才有作用:@ExceptionHandler、@InitBinder、@ModelAttribute。

如果想使用@ExceptionHandler(全局异常)、@InitBinder(请求方法之前的初始化)、@ModelAttribute(全局获取部门数据),同时他们三个运用也必须要有@RestControllerAdvice ,可以理解为为RequestController 和@Component合体的实现

@ExceptionHandler 全局异常配置

@ExceptionHandler一般做全局异常处

package com.dxt.common.exception;


import com.dxt.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ClassName: ComponentExceptionHandler
 * Package: com.dxt.dispatch.config.exception
 * Description: 全局异常处理
 * Date: 2023/3/28 15:17
 * Author: A乐神
 */
@RestControllerAdvice
@Slf4j
public class CommonExceptionHandler {

    private final Pattern pattern = Pattern.compile(".*[a-zA-Z]+.*");


    /**
    * Description: 手动抛出的业务异常,即般情况下不用关心的异常
    * date: 2023/3/28 15:30
    * @author : A乐神
    */
    @ExceptionHandler(value = BusinessException.class)
    public Result<?> businessExceptionHandler(BusinessException e) {
        log.error("BusinessException", e);
        return Result.error(e.getMessage());
    }

    /**
    * Description: 手动抛出(非业务异常)或者其他应用抛出的执行时异常,
     * 如果有msg 纯中文可以返回给用户否则不返回给用户,总是打印全部错误内容
    * date: 2023/3/28 15:31
    * @author : A乐神
    */
    @ExceptionHandler(value = Exception.class)
    public Result<?> exceptionHandler(Exception e) {
        String message = e.getMessage();
        log.error("RuntimeException", e);
        if (StringUtils.isNotBlank(message)) {
            // 匹配不到英文则返回错误信息
            Matcher matcher = pattern.matcher(message);
            if (!matcher.matches()) {
                return Result.error(message);
            }
        }
        return Result.error();
    }
}

public class BusinessException extends RuntimeException {
    public BusinessException() {
    }

    public BusinessException(String message) {
        super(message);
    }
}

@ModelAttribute

把值绑定到Model中,使全局@RequestMapping可以获取到该值

    @ModelAttribute
    public void addAttributes(Model model) {
        model.addAttribute("author", "Magical Sam");
    }

获取方式

@RequestMapping("/home")
public String home(ModelMap modelMap) {
    System.out.println(modelMap.get("author"));
}

//或者 通过@ModelAttribute获取

@RequestMapping("/home")
public String home(@ModelAttribute("author") String author) {
    System.out.println(author);
}

@InitBinder 全局请求方法前置处理(可以不看)

所有@RequestMapping注解方法,在其执行之前初始化数据绑定器,一般没人用

 @InitBinder
    public void initBinder(WebDataBinder binder) {}
  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A乐神

恭喜发财啊,老板,嘻嘻!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值