最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot增強(qiáng)Controller方法@ControllerAdvice注解的使用詳解

 更新時(shí)間:2023年10月12日 10:31:19   作者:fengyehongWorld  
這篇文章主要介紹了SpringBoot增強(qiáng)Controller方法@ControllerAdvice注解的使用詳解,@ControllerAdvice,是Spring3.2提供的新注解,它是一個(gè)Controller增強(qiáng)器,可對(duì)controller進(jìn)行增強(qiáng)處理,需要的朋友可以參考下

一. @ControllerAdvice注解作用

@ControllerAdvice ,是Spring3.2提供的新注解,它是一個(gè)Controller增強(qiáng)器,可對(duì)controller進(jìn)行增強(qiáng)處理。

  • 配合@ExceptionHandler注解,進(jìn)行全局異常處理。
  • 配合@InitBinder注解,用來(lái)設(shè)置WebDataBinder,用于自動(dòng)綁定前臺(tái)請(qǐng)求參數(shù)到Model中,全局?jǐn)?shù)據(jù)預(yù)處理,多用于表單提交數(shù)據(jù)或者url傳參。
  • 配合@ModelAttribute注解,讓Controller類(lèi)中所有的方法都可以獲取到通過(guò)@ModelAttribute注解設(shè)置的值,進(jìn)行全局?jǐn)?shù)據(jù)綁定。

注意: @ControllerAdvice 注解將作用在所有Controller層的方法上。

二. @ControllerAdvice注解 + assignableTypes屬性

作用:增強(qiáng)指定 .class 的類(lèi),非指定的Controller類(lèi)不會(huì)被增強(qiáng)

2.1 @ControllerAdvice增強(qiáng)類(lèi)

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;
// 指定增強(qiáng)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類(lèi)中的Exception異常
    @ExceptionHandler(Exception.class)
    public void exceptionHandle(Exception ex) {
        System.out.println(ex.getMessage());
    }
}

2.2 Controller層

目錄結(jié)構(gòu)如下

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

說(shuō)明

  • @ControllerAdvice增強(qiáng)類(lèi)中,指定對(duì)S001LoginController.java和S002LogoutController.java進(jìn)行了增強(qiáng),因此這兩個(gè)類(lèi)中可以獲取到增強(qiáng)類(lèi)中放入的數(shù)據(jù),并且這兩個(gè)類(lèi)中拋出的Exception異常也會(huì)被增強(qiáng)類(lèi)所捕獲。
  • 由于S003Controller.java并沒(méi)有被增強(qiáng),因此該類(lèi)無(wú)法獲取增強(qiáng)類(lèi)中放入的數(shù)據(jù),并且拋出的異常也無(wú)法被增強(qiáng)類(lèi)捕獲。

S001LoginController.java

有2種方式可以獲取出@ControllerAdvice增強(qiáng)類(lèi)中放入Model中的數(shù)據(jù)

  1. model.asMap()
  2. @ModelAttribute("key名稱(chēng)") 類(lèi)型 變量名

還可以通過(guò)request.getAttribute("key名稱(chēng)")來(lái)獲取放入request的attribute中的數(shù)據(jù)。

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增強(qiáng)類(lèi)中提前放入Model中的數(shù)據(jù)
        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增強(qiáng)類(lèi)中提前放入request的attribute中的數(shù)據(jù)
        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增強(qiáng)類(lèi)中提前放入Model中的數(shù)據(jù)
            @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增強(qiáng)類(lèi)中提前放入Model中的數(shù)據(jù)
        Map<String, Object> mapInfo = model.asMap();
        System.out.println(mapInfo);
		// 代碼出現(xiàn)異常
        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 效果

獲取數(shù)據(jù)

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

異常

在這里插入圖片描述

三. @ControllerAdvice注解 + basePackages屬性

增強(qiáng)指定包下面所有的Controller

3.1 @ControllerAdvice增強(qiáng)類(lèi)

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屬性

增強(qiáng)標(biāo)記了 指定注解 的所有的Controller

4.1 自定義注解

import java.lang.annotation.*;
@Documented
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ControllerMarkAnnotation {
}

4.2 @ControllerAdvice增強(qiáng)類(lèi)

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());
    }
}

到此這篇關(guān)于SpringBoot增強(qiáng)Controller方法@ControllerAdvice注解的使用詳解的文章就介紹到這了,更多相關(guān)SpringBoot的@ControllerAdvice注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

平潭县| 通山县| 昂仁县| 普洱| 通城县| 湄潭县| 福建省| 西乌珠穆沁旗| 深圳市| 潜山县| 长丰县| 定日县| 蒙山县| 连平县| 广灵县| 武平县| 丹棱县| 余庆县| 铁岭市| 四平市| 霍城县| 建始县| 南部县| 津市市| 麻阳| 错那县| 曲松县| 玉树县| 陈巴尔虎旗| 淮滨县| 芦溪县| 南皮县| 确山县| 济源市| 凉城县| 普宁市| 东台市| 余庆县| 义马市| 古蔺县| 丹寨县|