扩展:自定义异常返回
/**
* 自定义异常返回页面
*/
@Component
public class ExceptionHandlerPage implements UrlBlockHandler {
@Override
public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException e) throws IOException {
//BlockException 异常接口,包含Sentinel的五个异常
//FlowException 限流异常
//DegradeException 降级异常
//ParamFlowException 参数限流异常
//AuthorityException 授权异常
//SystemBlockException 系统负载异常
response.setContentType("application/json;charset=UTF-8");
ResponseData responseData = null;
if (e instanceof FlowException) {
responseData = new ResponseData(-1, "接口被限流了...");
} else if (e instanceof DegradeException) {
responseData = new ResponseData(-2, "接口被降级了...");
} else if (e instanceof ParamFlowException) {
responseData = new ResponseData(-3, "接口被参数限流了...");
} else if (e instanceof AuthorityException) {
responseData = new ResponseData(-4, "接口授权异常了...");
} else if (e instanceof SystemBlockException) {
responseData = new ResponseData(-5, "接口系统负载异常了...");
}
response.getWriter().write(JSON.toJSONString(responseData));
}
}
@Data
@AllArgsConstructor //全参构造
@NoArgsConstructor
//无参构造
class ResponseData {
private int code;
private String message;
}
网友评论