@controlleradvice註解作用

SpringMVC 中 @ControllerAdvice 註解的三種使用場景!

@ControllerAdvice ,很多初學者可能都沒有聽說過這個註解,實際上,這是一個非常有用的註解,顧名思義,這是一個增強的 Controller。使用這個 Controller ,可以實現三個方面的功能:

  1. 全局異常處理  結合註解 @ExceptionHandler 使用
  2. 全局數據綁定 @ModelAttribute
  3. 全局數據預處理 @ModelAttribute ,@InitBinder

靈活使用這三個功能,可以幫助我們簡化很多工作,需要注意的是,這是 SpringMVC 提供的功能,在 Spring Boot 中可以直接使用,下面分別來看。

全局異常處理

使用 @ControllerAdvice 實現全局異常處理,只需要定義類,添加該註解即可定義方式如下:

@ControllerAdvice
public class MyGlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ModelAndView customException(Exception e) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", e.getMessage());
        mv.setViewName("myerror");
        return mv;
    }
}

在該類中,可以定義多個方法,不同的方法處理不同的異常,例如專門處理空指針的方法、專門處理數組越界的方法...,也可以直接向上面代碼一樣,在一個方法中處理所有的異常信息。

@ExceptionHandler 註解用來指明異常的處理類型,即如果這裏指定爲 NullpointerException,則數組越界異常就不會進到這個方法中來。

全局數據綁定

全局數據綁定功能可以用來做一些初始化的數據操作,我們可以將一些公共的數據定義在添加了 @ControllerAdvice 註解的類中,這樣,在每一個 Controller 的接口中,就都能夠訪問導致這些數據。

使用步驟,首先定義全局數據,如下:

@ControllerAdvice
public class MyGlobalExceptionHandler {
    @ModelAttribute(name = "md")
    public Map<String,Object> mydata() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("age", 99);
        map.put("gender", "男");
        return map;
    }
}

使用 @ModelAttribute 註解標記該方法的返回數據是一個全局數據,默認情況下,這個全局數據的 key 就是返回的變量名,value 就是方法返回值,當然開發者可以通過 @ModelAttribute 註解的 name 屬性去重新指定 key。

定義完成後,在任何一個Controller 的接口中,都可以獲取到這裏定義的數據:

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello(Model model) {
        Map<String, Object> map = model.asMap();
        System.out.println(map);
        int i = 1 / 0;
        return "hello controller advice";
    }
}

全局數據預處理

考慮我有兩個實體類,Book 和 Author,分別定義如下:

public class Book {
    private String name;
    private Long price;
    //getter/setter
}
public class Author {
    private String name;
    private Integer age;
    //getter/setter
}

此時,如果我定義一個數據添加接口,如下:

@PostMapping("/book")
public void addBook(Book book, Author author) {
    System.out.println(book);
    System.out.println(author);
}

這個時候,添加操作就會有問題,因爲兩個實體類都有一個 name 屬性,從前端傳遞時 ,無法區分。此時,通過 @ControllerAdvice 的全局數據預處理可以解決這個問題

解決步驟如下:

1.給接口中的變量取別名

@PostMapping("/book")
public void addBook(@ModelAttribute("b") Book book, @ModelAttribute("a") Author author) {
    System.out.println(book);
    System.out.println(author);
}

2.進行請求數據預處理
在 @ControllerAdvice 標記的類中添加如下代碼:

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

@InitBinder("b") 註解表示該方法用來處理和Book和相關的參數,在方法中,給參數添加一個 b 前綴,即請求參數要有b前綴.

3.發送請求

請求發送時,通過給不同對象的參數添加不同的前綴,可以實現參數的區分.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章