@Restcontrolleradvice is a composite annotation
@ControllerAdvice
@ResponseBody
import org.springframework.web.bind.annotation.RestControllerAdvice;
Function: enhance the controller, match the two, and often handle global exceptions
@Controlleradvice works on classes in three ways
Global exception handling: @ModelAttribute
Global data binding: @ModelAttribute
Global data preprocessing: @InitBinder
Global exception handling
@RestControllerAdvice
public class GlobalExceptionDeal {
@ExceptionHandler(NullPointerException.class)
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseBean dealNullPointerException() {
ResponseBean responseBean = new ResponseBean(500,"Server exception - null pointer exception");
return responseBean;
}
@ExceptionHandler(Exception.class)
@ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseBean dealUnexpectedException(){
ResponseBean responseBean=new ResponseBean(505,"Server exception - unknown error");
return responseBean;
}
}
Similar Posts:
- Accessdeniedhandler configured in spring security does not take effect
- Hyperf captures PHP error levels, such as notice, warning and error, and returns them to the front-end
- Quartz: add transaction rollback error [How to Solve]
- [Solved] Using JDK dynamic agent to customize SPI Error: UndeclaredThrowableException
- PHP Fatal error: Cannot redeclare class
- dynamic_cast Error: source type is not polymorphic
- How to Solve Spring Bean Same Name Conflict
- [Problems with the use of internal classes] No enclosing instance of type Outer is accessible. Must qualify the allocation with an enclo
- [Solved] UserInfo()’ in ‘com.example.gmall.cart.pojo.UserInfo’ cannot be applied to ‘(long, java.lang.String)’
- Java error: No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing