Spring Boot @ControllerAdvice三大功能

本文详细介绍了@ControllerAdvice在Spring MVC中的三大功能:全局异常处理、数据绑定和数据预处理。通过实例展示如何捕获并返回异常信息,定制异常响应,以及如何使用@ModelAttribute进行数据预处理,确保前后端数据一致。
摘要由CSDN通过智能技术生成

@ControllerAdvice三大功能

  • 全局异常处理
  • 全局数据绑定
  • 全局数据预处理

全局异常处理

  • @RestControllerAdvice ----- @RestController 返回字符串
  • @ControllerAdvice ----- @Controller 返回视图

1.拦截所有异常,并把异常信息返回至浏览器页面上

spring.servlet.multipart.max-file-size=20KB
@RestControllerAdvice
public class MyGlobalException {
    @ExceptionHandler(Exception.class)
    public String customException(Exception e){
        return e.getMessage();
    }
}

在这里插入图片描述
2.拦截特定异常,并返回特定字符
添加其他异常:

@GetMapping("/errorTest")
    public void erroTest(){
        int i = 1/0;
    }

特定异常处理:

@RestControllerAdvice
public class MyGlobalException {
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public String customException(MaxUploadSizeExceededException e){
        return "文件大小超过限制";
    }
}

在这里插入图片描述
在这里插入图片描述

    @ExceptionHandler(ArithmeticException.class)
    public String customException1(ArithmeticException e){
        return "数学逻辑错误";
    }

在这里插入图片描述
3.拦截异常,返回视图
定义视图:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>dong</h1>
<div th:text="${error}"></div>
</body>
</html>

拦截异常:

@ControllerAdvice
public class MyGlobalException {
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView customException(MaxUploadSizeExceededException e){
        ModelAndView view = new ModelAndView("dong");
        view.addObject("error",e.getMessage());
        return view;
    }
}

在这里插入图片描述

全局数据绑定

定义数据绑定类:

@ControllerAdvice
public class GetGlobalVeriables {
    @ModelAttribute
    public Map<String,String> getData(){
        Map<String,String> info = new HashMap<>();
        info.put("username","dong");
        info.put("password","123");
        return info;
    }
}

定义测试类:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public void getData(Model model){
        Map<String, Object> asmap = model.asMap();
        Map<String, String> map = (Map<String, String>) asmap.get("map");
        Set<String> keySet = map.keySet();
        for (String s : keySet) {
            System.out.println(s+"-----"+map.get(s));
        }
    }
}

在这里插入图片描述

全局数据预处理

测试类:
Autor类:

public class Autor {
    private String name;
    private Integer age;
    get和set方法...
}

book类:

public class Book {
    private String name;
    private Integer price;
    get和set方法...
}

Controller:

@RestController
public class BookController {
    @PostMapping("/book")
    public void getbook(Book book,Autor autor){
        System.out.println("book = " + book);
        System.out.println("autor = " + autor);
    }
}

Postman中Post请求:
在这里插入图片描述
结果并不理想:
在这里插入图片描述
我们发现两个类的属性并没有很好的区分
解决方法:
Controller类:

@RestController
public class BookController {
    @PostMapping("/book")
    public void getbook(@ModelAttribute("b") Book book, @ModelAttribute("a") Autor autor){
        System.out.println("book = " + book);
        System.out.println("autor = " + autor);
    }
}

GetGlobalVeriables类:

@ControllerAdvice
public class GetGlobalVeriables {
    @InitBinder("b")
    public void bindMethod(WebDataBinder binder){
        binder.setFieldDefaultPrefix("b.");
    }
    @InitBinder("a")
    public void abindMethod(WebDataBinder binder){
        binder.setFieldDefaultPrefix("a.");
    }
}

Post请求:
在这里插入图片描述
结果正确:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值