代码定义
package com.example.demo.common; public class ServiceException extends RuntimeException { private static final long seriaVersionUID = 1L; private String message; @Override public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public ServiceException(final String message) { this.message = message; } public ServiceException(final String message, Throwable th) { super(message, th); this.message = message; } public static void throwEx(String message){ throw new ServiceException(message); } }
使用
@ApiOperation("登录接口")
@PostMapping("login")
public String login(@RequestBody UserDto userDto){
String result = userService.login(userDto);
if(userDto.getName().contains("error2")){
throw new ServiceException("error2",new NullPointerException());
}
if(userDto.getName().contains("error")){
ServiceException.throwEx("用户名中含有error");
}
return "成功" + result;
}
一、postman发送error

服务端日志

二、postman发送error2

服务端日志
