One ,ControllerAdvice Common scenes of

1, Global exception handling

2, Global data binding

3, Global data preprocessing

Use these three functions flexibly , It can help us simplify a lot of work , It should be noted that , This is SpringMVC Functions provided , stay Spring Boot Can be used directly in , Let's look at it separately .

Two , Global exception handling

use @ControllerAdvice Implement global exception handling , Just define the class , Add the annotation to define it as follows :
package com.atguigu.commonutils.exceptionhandler; import
com.atguigu.commonutils.R; import
org.springframework.web.bind.annotation.ControllerAdvice; import
org.springframework.web.bind.annotation.ExceptionHandler; import
org.springframework.web.bind.annotation.ResponseBody; @ControllerAdvice public
class GlobalExceptionHandler { @ExceptionHandler(Exception.class) @ResponseBody
// In order to be able to return data public R error(Exception e){ e.printStackTrace(); return
R.error().message(" Global exception handling performed "); } @ExceptionHandler(ArithmeticException.class)
@ResponseBody // In order to be able to return data public R error(ArithmeticException e){
e.printStackTrace(); return R.error().message(" Implemented ArithmeticException exception handling "); } }
In this class , You can define multiple methods , Different methods handle different exceptions , For example, the method of handling all exceptions , The method of dealing with arithmetic operation exception ...

@ExceptionHandler Annotations are used to indicate the type of exception handling , That is, if the
ArithmeticException, The array out of bounds exception will not enter this method .

Three , Global data binding

The global data binding function can be used to do some initialization data operations , We can add some common data definitions in the @ControllerAdvice Annotated classes , such , In every
Controller In the interface of , You can access the data that leads to this .

Use steps , First, define the global data , as follows :
@ControllerAdvice public class MyGlobalExceptionHandler { @ModelAttribute(name
= "md") public Map<String,Object> mydata() { Map<String, Object> map = new
HashMap<>(); map.put("id", 23); map.put("name", " Su xiaonuan "); return map; } }
use @ModelAttribute Annotation marks that the return data of this method is a global data , By default , The global data key Is the name of the returned variable ,value
Is the return value of the method , Of course, developers can @ModelAttribute Annotated name Property to reassign key.

After definition , In any one Controller In the interface of , You can get the data defined here :
@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"; } }
Four , Global data preprocessing

Consider that I have two entity classes ,Book and Author, They are defined as follows :
public class Book { private String name; private Long price; //getter/setter }
public class Author { private String name; private Integer age; //getter/setter
here , If I define a data addition interface , as follows :
@PostMapping("/book") public void addBook(Book book, Author author) {
System.out.println(book); System.out.println(author); }
At this time , Add operation will have problems , Because both entity classes have one name attribute , When passing from the front end , Can't tell . here , adopt @ControllerAdvice
Global data preprocessing can solve this problem

The solution steps are as follows :

1, Aliasing variables in an interface
@PostMapping("/book") public void addBook(@ModelAttribute("b") Book book,
@ModelAttribute("a") Author author) { System.out.println(book);
System.out.println(author); }
2, Preprocess the request data

stay @ControllerAdvice Add the following code to the marked class :
@InitBinder("b") public void b(WebDataBinder binder) {
binder.setFieldDefaultPrefix("b."); } @InitBinder("a") public void
a(WebDataBinder binder) { binder.setFieldDefaultPrefix("a."); }
@InitBinder("b") Annotations indicate that the method is used to handle and Book And related parameters , In the method , Add a b prefix , That is, the request parameters should have b prefix .

3, Send request

When the request is sent , By adding different prefixes to the parameters of different objects , The parameters can be distinguished .


©2019-2020 Toolsou All rights reserved,
html Writing about cherry trees , Writing about cherry trees It's unexpected Python Cherry tree (turtle The gorgeous style of Library ) Browser kernel ( understand )HashMap Explain in detail java Four functional interfaces ( a key , simple )os Simple use of module Some East 14 Pay change 16 salary , Sincerity or routine ?