SpringBoot使用自定义异常
SpringBoot使用自定义异常
1.自定义异常
public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}
2.处理自定义异常
@ControllerAdvice
public class GlobalExceptionHandler {
// 处理自定义异常
@ExceptionHandler(CustomException.class)
public ResponseEntity<Object> handleCustomException(CustomException ex) {
Map<String, Object> body = new HashMap<>();
body.put("code", 200);
body.put("msg", ex.getMessage());
body.put("data", data);
return new ResponseEntity<>(body, HttpStatus.OK);
}
分类:
标签:
SpringBoot 自定义异常
版权申明
本文系作者 @卸了磨的驴 原创发布在SpringBoot使用自定义异常。未经许可,禁止转载。
全部评论