SpringBoot 增强Controller方法,@ControllerAdvice注解的使用

参考资料

  1. @ControllerAdvice 用法
  2. SpringBoot使用@ControllerAdvice注解


一. @ControllerAdvice注解作用

@ControllerAdvice,是Spring3.2提供的新注解,它是一个Controller增强器,可对controller进行增强处理。

  • 配合@ExceptionHandler注解,进行全局异常处理。
  • 配合@InitBinder注解,用来设置WebDataBinder,用于自动绑定前台请求参数到Model中,全局数据预处理,多用于表单提交数据或者url传参。
  • 配合@ModelAttribute注解,让Controller类中所有的方法都可以获取到通过@ModelAttribute注解设置的值,进行全局数据绑定。

❗❗❗注意@ControllerAdvice注解将作用在所有Controller层的方法上。

在这里插入图片描述


二. @ControllerAdvice注解 + assignableTypes属性

⏹作用:增强指定.class的类,非指定的Controller类不会被增强

2.1 @ControllerAdvice增强类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 指定增强S001LoginController和S002LogoutController
@ControllerAdvice(assignableTypes = {
    
    S001LoginController.class, S002LogoutController.class})
public class S_Controller {
    
    

    @Autowired
    private HttpServletRequest request;
	
	// 方式1: @ModelAttribute注解的使用
    @ModelAttribute
    public void initData1(Model model) {
    
    
        request.setAttribute("num1", 66);
        model.addAttribute("userMail", List.of("[email protected]", "[email protected]"));

    }
	
	// 方式2: @ModelAttribute注解的使用
    @ModelAttribute(name = "userInfo")
    public Map<String, String> initData2() {
    
    
        request.setAttribute("num2", 99);
       return new HashMap<>(){
    
    
           {
    
    
               put("name", "贾飞天");
               put("age", "18");
           }
       };
    }
	
	// 捕获S001LoginController或S002LogoutController类中的Exception异常
    @ExceptionHandler(Exception.class)
    public void exceptionHandle(Exception ex) {
    
    

        System.out.println(ex.getMessage());
    }
}

2.2 Controller层

⏹目录结构如下

  • s_controller包
    • S001LoginController.java
    • S002LogoutController.java
    • S003Controller.java

⏹说明

  • @ControllerAdvice增强类中,指定对S001LoginController.javaS002LogoutController.java进行了增强,因此这两个类中可以获取到增强类中放入的数据,并且这两个类中抛出的Exception异常也会被增强类所捕获。
  • 由于S003Controller.java并没有被增强,因此该类无法获取增强类中放入的数据,并且抛出的异常也无法被增强类捕获。

⭕S001LoginController.java

  • 有2种方式可以获取出@ControllerAdvice增强类中放入Model中的数据
    • model.asMap()
    • @ModelAttribute("key名称") 类型 变量名
  • 还可以通过request.getAttribute("key名称")来获取放入requestattribute中的数据。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ss001")
public class S001LoginController {
    
    

    @Autowired
    private HttpServletRequest request;

    @GetMapping("/init")
    public ModelAndView init(Model model) {
    
    
		
		// ⭕方式1: 获取出@ControllerAdvice增强类中提前放入Model中的数据
        Map<String, Object> mapInfo = model.asMap();

        Object userMailObj = mapInfo.get("userMail");
        List<String> userMailList = (List<String>)userMailObj;
        System.out.println(userMailList);  // [[email protected], [email protected]]

        Map<String, String> userInfoMap = (Map<String, String>) mapInfo.get("userInfo");
        System.out.println(userInfoMap);  // {name=贾飞天, age=18}
		
		// ⭕获取出@ControllerAdvice增强类中提前放入request的attribute中的数据
        Integer num1 = (Integer) request.getAttribute("num1");
        System.out.println(num1);  // 66

        Integer num2 = (Integer) request.getAttribute("num2");
        System.out.println(num2);  // 99

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("s001");
        return modelAndView;
    }

    @GetMapping("/showInfo")
    @ResponseBody
    public void showInfo(
    		// ⭕方式2: 获取出@ControllerAdvice增强类中提前放入Model中的数据
            @ModelAttribute("userMail") List<String> userMail
            , @ModelAttribute("userInfo") Map<String, String> userInfo
    ) {
    
    
        System.out.println(userMail);  // [[email protected], [email protected]]
        System.out.println(userInfo);  // {name=贾飞天, age=18}
    }
}

⭕S002LogoutController.java

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
@RequestMapping("/ss002")
public class S002LogoutController {
    
    

    @GetMapping("/init")
    public ModelAndView init(Model model) {
    
    
		
		// 获取出@ControllerAdvice增强类中提前放入Model中的数据
        Map<String, Object> mapInfo = model.asMap();
        System.out.println(mapInfo);
		
		// 代码出现异常
        int num = 1 / 0;

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("s002");
        return modelAndView;
    }
}

⭕S003Controller.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/ss003")
public class S003Controller {
    
    

    @GetMapping("/init")
    public ModelAndView init() {
    
    

        int num = 1 / 0;

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("s003");
        return modelAndView;
    }
}

2.3 效果

⏹获取数据

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

⏹异常

在这里插入图片描述

三. @ControllerAdvice注解 + basePackages属性

  • 增强指定包下面所有的Controller

3.1 @ControllerAdvice增强类

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice(basePackages = {
    
    "com.example.jmw.t_controller"})
public class T_Controller {
    
    

    @ExceptionHandler(Exception.class)
    public void exceptionHandle(Exception ex) {
    
    

        System.out.println(ex.getMessage());
    }
}

四. @ControllerAdvice注解 + annotations属性

  • 增强标记了指定注解的所有的Controller

4.1 自定义注解

import java.lang.annotation.*;

@Documented
@Target({
    
    ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ControllerMarkAnnotation {
    
    

}

4.2 @ControllerAdvice增强类

mport org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice(annotations = ControllerMarkAnnotation.class)
public class X_Controller {
    
    

    @ExceptionHandler(Exception.class)
    public void exceptionHandle(Exception ex) {
    
    

        System.out.println(ex.getMessage());
    }
}

猜你喜欢

转载自blog.csdn.net/feyehong/article/details/131028588