In the project, there are often some businesses that need to throw exceptions, but if the background directly throws throw new Exception , the front end will be ugly, and the user prompts are not friendly enough. Today we will solve this problem.
Create a project first, and simulate throwing an exception. as follows:
@RestController
public class DemoController {
@GetMapping("test")
public String test() throws Exception{
if(true){
throw new Exception("error");
}
return "ok";
}
}
The front end uses a browser request to see what the interface looks like:
@ControllerAdvice and @ExceptionHandler
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public ResultDto globalException(HttpServletResponse response, Exception ex){
log.info("ExceptionHandler...");
log.info("错误代码:" + response.getStatus());
ResultDto resultDto = new ResultDto();
resultDto.setCode(0);
resultDto.setMsg(ex.getMessage());
resultDto.setData(null);
return resultDto;
}
}
Define a general structure back to the front end
@Data
public class ResultDto {
//请求结果0表示失败,其他是成功
private int code;
//失败的消息
private String msg;
//实际返回到前端的数据
private Object data;
}
Then we write a controller simulate throwing an exception
@GetMapping("test1")
public String test1() throws Exception{
if(true){
throw new NullPointerException("NullPointerException");
}
return "ok";
}
@GetMapping("test2")
public String test2() throws Exception{
if(true){
throw new RuntimeException("RuntimeException");
}
return "ok";
}
@GetMapping("test3")
public String test3() throws MyException{
if(true){
//不能直接拋Exception 否则不能捕获,可以自己定义一个异常
throw new MyException("MyException");
}
return "ok";
}
request, see what 1610797fdd7cba postman returns
{
"code": 0,
"msg": "NullPointerException",
"data": null
}
In actual business, we usually return custom exceptions. Then I will define an own exception and deal with it separately:
public class MyException extends Exception {
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
public MyException(String message, Throwable cause) {
super(message, cause);
}
public MyException(Throwable cause) {
super(cause);
}
protected MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Then add the processing MyException GlobalExceptionHandler
@ExceptionHandler(MyException.class)
@ResponseBody
@ResponseStatus(HttpStatus.OK)
public ResultDto myException(HttpServletResponse response, MyException ex){
log.info("MyExceptionHandler...");
log.info("错误代码:" + response.getStatus());
ResultDto resultDto = new ResultDto();
resultDto.setCode(0);
resultDto.setMsg(ex.getMessage());
resultDto.setData(null);
return resultDto;
}
Request http://localhost:8080/test3
see what is output to the front end
{
"code": 0,
"msg": "MyException",
"data": null
}
In fact, I can't tell whether myException or globalException but if you look at the background log output, you can clearly see it. Therefore, when we deal with multiple exceptions, springboot will give priority to subclass exceptions.
More java original reading: https://javawu.com
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。