premise
Why use global exception handling?
Because of various exceptions encountered in the project development process, if we deal with them separately, the performance is low and inflexible. What the user shows is a bunch of English prompts, so we deal with this kind of exception at once, so we need global exception handling ,
ready
First, we create an exception package, and create a class in the package
我们的这个异常处理配合Lombok来配合处理
这个类是我们的全局异常类,
Simple global exception
@Slf4j //lombok中的日志处理注解
@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 processing 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:
The first step: first we create a package, create a class in the package
The second part: enter the definition of log annotations and global exception annotations above the class
Step 3: Define the exception handling annotations handled by the controller layer in the class
Step 4: Write the exception type to be handled in the exception handling annotation
Step 5: Create an object whose return value is String type in the class
Step 6: If there is an abnormality in the log written in the object, print it
Step 7: return returns the printed result
Responsive standard design
What is the response standard?
Our data is sent to the client, and we design the state of these data.
If the data in response now contains three types such as: status, message, specific data, etc...
premise
First we have to create a new package, and then create classes in the package
Design response data
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class viewTable<T> {
private Integer state=1;//状态码 表示1 ok,表示0 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();
}
}
我们基于Lombak来实现
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@Data--This annotation Lombak's annotation automatically generates get, set and tostring
@Accessors--The role of this annotation The method names of getter and setter methods are attribute names
@NoArgsConstructor--Generate a no-argument construct
@AllArgsConstructor-generate parameterized constructs
First, we specify the form of data displayed on the page
private Integer state=1;//状态码 表示1 ok,表示0 error
private String message;//状态信息
private List<T> rows; //封装查询结果
state-state to the page to send 1 to indicate normal and 0 to indicate error
message – If the status information is successful, it will be ok and an error will be reported
rows-the data we display
The following is the type package
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 one — if the returned result is a string
The second one — if the returned result is the query data
The third — if the returned data is abnormal
这样我们给页面显示的数据成功封装好了
Global exception handling (advanced)
Preface
Now we respond to the data structure and primary exception handling. Next we will combine the two
@Slf4j //lombok中的日志处理注解
@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("数据库异常");
}else if (e instanceof RuntimeException){
v.setMessage("服务器异常");
}
log.error("RuntimeException.exception {}",e.getMessage());
return v;
}
}
Explanation
-
We change the type of the class to the type of our response structure is viewTable
-
We new a response class that we just designed
-
Get the status code inside it is designed to be 0
-
We have to determine if the abnormality is about SQL, then send the database abnormality to the client
-
If the exception is about Runtime, then send the server exception to the client
-
Are the exceptions these two, so the client sends the information of the exception itself
-
Later return to the class of the response page