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("123@mail.com", "456@mail.com"));

    }
	
	// 方式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);  // [123@mail.com, 456@mail.com]

        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);  // [123@mail.com, 456@mail.com]
        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());
    }
}
@Path注解是JAX-RS规范中的注解,而Spring Boot是基于Spring框架的Web应用程序框架,两者并不是直接相关的。在Spring Boot中,通常使用@ControllerAdvice注解来定义全局异常处理器,它可以捕获所有Controller中抛出的异常,并根据异常类型或其他条件进行处理。 如果您的应用程序同时使用JAX-RS和Spring Boot,您可以使用Spring Boot@ControllerAdvice注解来定义全局异常处理器,但是您需要使用JAX-RS的ExceptionMapper将JAX-RS的异常映射到Spring Boot的异常处理器中。 例如,您可以创建一个类并使用@Provider注解将它标记为JAX-RS提供程序,并实现ExceptionMapper接口,将JAX-RS的异常映射到Spring Boot的异常处理器中,如下所示: ``` @Provider public class MyExceptionMapper implements ExceptionMapper<Exception> { @Override public Response toResponse(Exception exception) { // 将JAX-RS的异常映射到Spring Boot的异常处理器中 return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Internal server error").build(); } } ``` 然后,您可以使用@ControllerAdvice注解来定义Spring Boot的全局异常处理器,并处理所有异常,例如: ``` @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<ErrorResponse> handleException(Exception ex) { ErrorResponse errorResponse = new ErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage()); return new ResponseEntity<>(errorResponse, HttpStatus.INTERNAL_SERVER_ERROR); } } ``` 需要注意的是,JAX-RS和Spring Boot的异常处理器是两个不同的机制,需要通过ExceptionMapper将它们连接起来。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值