Global exception handling :

Define a processing class , use @ControllerAdvice annotation .

@ControllerAdvice annotation : Controller enhancements , A quilt @Component Registered components .

coordination @ExceptionHandler To enhance all @requestMapping method .

for example :@ExceptionHandler(Exception.class)   To capture @requestMapping All the thrown exception.

code :
@ControllerAdvice public class GlobalDefultExceptionHandler { // Declare exception to catch
@ExceptionHandler(Exception.class) @ResponseBody public String
defultExcepitonHandler(HttpServletRequest request,Exception e) {     return
“error”; } }
such , Global exception handling class completed . You can add your own logic .

And then there's a question , sometimes , Throw custom exception when we need business logic , You need to customize the business exception class at this time .

definition class:BusinessException , Make him inherit from RuntimeException.

explain : Because some businesses need business rollback . but spring For RuntimeException To roll back . So if you need to roll back, you need to inherit RuntimeException.
public class BusinessException extends RuntimeException{ }
then , Now let's refine this class a little bit .

When we throw a business exception , Error codes and error messages are generally required . It helps us to locate the problem .

So here's how :
public class BusinessException extends RuntimeException{ // Custom error code private
Integer code; // Custom constructor , Keep only one , It is required to enter error code and content public BusinessException(int
code,String msg) { super(msg); this.code = code; } public Integer getCode() {
return code; } public void setCode(Integer code) { this.code = code; } }
At this time , We found another problem , If you write like this , After more code , It is difficult to manage the match between these business exceptions and error codes . So we are optimizing it .

Put error code and error information , Integrated management .

Define an enumeration of business exceptions .
public enum ResultEnum { UNKONW_ERROR(-1," unknown error "), SUCCESS(0," success "),
ERROR(1," fail "), ; private Integer code; private String msg; ResultEnum(Integer
code,String msg) { this.code = code; this.msg = msg; } public Integer getCode()
{ return code; } public String getMsg() { return msg; } }
This time , Business exception class :
public class BusinessException extends RuntimeException{ private static final
long serialVersionUID = 1L; private Integer code; // Error code public
BusinessException() {} public BusinessException(ResultEnum resultEnum) {
super(resultEnum.getMsg()); this.code = resultEnum.getCode(); } public Integer
getCode() { return code; } public void setCode(Integer code) { this.code =
code; } }
Then modify the global exception handling class :
@ControllerAdvice public class GlobalDefultExceptionHandler { // Declare exception to catch
@ExceptionHandler(Exception.class) @ResponseBody public <T> Result<?>
defultExcepitonHandler(HttpServletRequest request,Exception e) {
e.printStackTrace(); if(e instanceof BusinessException) {
Log.error(this.getClass()," Business exception :"+e.getMessage()); BusinessException
businessException = (BusinessException)e; return
ResultUtil.error(businessException.getCode(), businessException.getMessage());
} // unknown error return ResultUtil.error(-1, " System exception :\\n"+e); } }
Determine whether this is a business exception . And system exceptions can be handled separately .

Technology
©2019-2020 Toolsou All rights reserved,
C Language programming to find a student's grade java Realize the function of grabbing red packets Random forest R Language implementation QCustomPlot series (5)- Real time dynamic curve Software testing BUG describe use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor )JS How to operate China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour Zimi apologizes :33W Gan charger delayed due to force majeure 2021 year 2 Chinese programming language ranking