Spring @ControllerAdvice注解

Spring @ControllerAdvice & @RestControllerAdvice

简介

  • @ControllerAdvice注解是 Spring对控制器进行切面环绕的
  • 返回 Json数据时使用 @RestControllerAdvice注释, 来代替 @ControllerAdvice

参数说明 (默认全包扫描

名称 说明
value com.example.controller 指定包, 即对指定包下的所有 Controller有效
basePackages com.example.controller 和参数 value相等
basePackageClasses com.example.controller.TestController.class 指定 Controller类, 扫描范围为包括当前类所在包内所有的其它 Controller类
assignableTypes TestController.class 指定具体的 Controller类
annotations {TestController01.class, TestController02.class} 可指定一个或多个 Controller类

基本使用例子

  1. 全局数据绑定

@ControllerAdvice(value = "com.example.controller")
public class TestControllerAdvice {
    @ModelAttribute("user")
    public User getUser() {
        User user = new User();
        user.setName("全爷");
        user.setAge(35);
        return user;
    }
}

@RestController
public class TestController {
    @GetMapping("/index")
    public Map<String, Object> index(@ModelAttribute("user") User user) {
        final Map<String, Object> result = new HashMap<>();
        result.put("name", user.getName());
        result.put("age", user.getAge());
        return result;
    }
}

# 请求地址 http://127.0.0.1:8080/index
# 输出
{"name":"全爷","age":35}

  1. 对全局请求数据预处理

public class Car {
    private String name;
    private String type;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

public class Driver {
    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

@ControllerAdvice(value = "com.example.controller")
public class TestControllerAdvice {
    @InitBinder("c")
    public void b(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("c.");
    }

    @InitBinder("d")
    public void a(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("d.");
    }
}

@RestController
public class TestController {
    @GetMapping("/index")
    public Map<String, Object> index(@ModelAttribute("c") Car car, @ModelAttribute("d") Driver driver) {
        final Map<String, Object> result = new HashMap<>();
        result.put("Car.name", car.getName());
        result.put("Car.type", car.getType());
        result.put("Driver.name", driver.getName());
        result.put("Driver.age", driver.getAge());
        return result;
    }
}

# 请求地址 http://127.0.0.1:8080/index?c.name=保时捷&c.type=赛跑&d.name=全爷&d.age=35
# 输出
{"Driver.age":35,"Car.type":"赛跑","Driver.name":"全爷","Car.name":"保时捷"}

  1. 全局异常捕获处理

# 自定义异常
public class CommonException extends RuntimeException {
    protected String status;
    protected String message;

    public CommonException() {}

    public CommonException(String status, String msg) {
        this.status = status;
        this.message = msg;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

# 捕获指定异常
@RestControllerAdvice(value = "com.example.controller")
public class TestControllerAdvice {
    @ExceptionHandler({CommonException.class})
    public Map<String, Object> customException(final CommonException e) {
        final Map<String, Object> result = new HashMap<>();
        result.put("status", e.getStatus());
        result.put("message", e.getMessage());
        return result;
    }
}

@RestController
public class TestController {
    @GetMapping("/index")
    public Map<String, Object> index() {
        if (true) {
            throw new CommonException("1000", "抛自定义异常!");
        }
        return new HashMap<>();
    }
}

# 请求地址 http://127.0.0.1:8080/index
# 输出
{"message":"抛自定义异常!","status":"1000"}

如果您觉得有帮助,欢迎点赞哦 ~ 谢谢!!

猜你喜欢

转载自blog.csdn.net/qcl108/article/details/105604648