The premise

Why use global exception handling?

Because we encountered various exceptions in the development process of our project, the performance was low and inflexible if we handled them separately. What the user displayed was a bunch of English prompts, so we handled such exceptions at once, so we needed global exception handling.

To prepare

First we create an exception package, inside which we create a class

Our exception handling works in conjunction with Lombok's handling

This class is our global exception class,

Simple global exceptions

@slf4j @restControllerAdvice @restControllerAdvice @slf4j @restControllerAdvice public class GolbalException { @ExceptionHandler(RuntimeException.class) @ResponseBody public String GolbalRuntimeException(RuntimeException e){ log.error("RuntimeException.exception {}",e.getMessage()); return e.getMessage(); }}
@slf4j (This annotation is Lombok’s log handling annotation)
@RestControllerAdvice(This annotation indicates that the current class is a global exception-handling class)
@ExceptionHandle(This annotation describes the exception method handled by the Controller layer)
@responseBody (This annotation describes the conversion of Java objects to JSON format)

Ideas:

Step 1: First we create a package that creates a class
Step 2: Go to the top of the class to define log annotations and global exception annotations
Step 3: Define exception handling annotations for the Controller layer in the class
Step 4: Exception Handling The annotation contains the type of exception to be handled
Step 5: Create an object whose return value is a String
Step 6: Write a log inside the object and print it if there is an exception
Step 7: return the result of the print

Response standard design

What are the response criteria?

Our data into the client, we carry on the state design to these data, if now the response data contains three kinds of such as: state, message, specific data, etc..

The premise

First we create the new package, and then we create the classes inside the package

Design response data

@Data @Accessors(chain = true) @NoArgsConstructor @AllArgsConstructor public class viewTable<T> { private Integer state=1; // Error private String message; // Error private String message; Private List<T> rows; Public viewTable(String message){this.message=message; } public viewTable(List<T> list) { this.rows=list; } public viewTable(Throwable e){ this.state=0; this.message=e.getMessage(); }}

We implemented it based on Lombak

@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor

@data — the annotation Lombak’s annotation automatically generates get and set and tostring@accessors — the function of this annotation. The method names of the getter and setter methods are both attribute names @noargsconstructor @AllArgsConstructor– Generates a constructor with arguments

First we specify the form of the data to be displayed on the page

private Integer state=1; // Error private String message; // Error private String message; Private List<T> rows; // Encapsulate the query results

State — State sends 1 to the page for normal 0 for error message — State message is successful ok error report exception rows — data we display

The following is type encapsulation

public viewTable(String message){
     this.message=message;
}
public viewTable(List<T> list) {
    this.rows=list;
}
public viewTable(Throwable e){
    this.state=0;
    this.message=e.getMessage();
}

The first — if the result is a string and the second — if the result is the query data and the third — if the result is an exception

So we have successfully encapsulated the data displayed on the page

Global exception handling (advanced)

preface

Now we’re going to respond to data structures and primary exception handling and we’re going to put both together

@slf4j // RestControllerAdvice @restControllerAdvice public class GolbalException<T> { @ExceptionHandler(Exception.class) @ResponseBody public viewTable GolbalRuntimeException(Exception e){ viewTable<T> v=new viewTable<>(); v.setState(0); If (E instanceof BadSQLGrammarException){v.setMessage(" Database Exception "); }else if (e Instanceof RuntimeException){v.setMessage(" server exception "); } log.error("RuntimeException.exception {}",e.getMessage()); return v; }}

explain

We change the type of the class to the type of our response structure which is ViewTable
Let’s create a new response class that we just designed
Gets the inside status code designed to be 0
We need to determine if the exception is about SQL then send a database exception to the client
If the exception is about Runtime, send the server exception to the client
Exceptions are either of these then the client sends the exception itself
Then return to the class of the response page

The results of

Exception that reported an error

SQL exceptions

Client display