怎么免费给网站做收录,有什么网站可以做平面兼职,河北网站制作公司电话,网站备案注意什么一、ERROR Filter
错误过滤器用来处理zuul异常#xff0c;一般使作为打印异常堆栈、跳转异常页面、转换异常信息格式返回等操作。
Zuul已定义的错误过滤器SendErrorFilter#xff0c;如果RequestContext.getThrowable()不为null#xff0c;则转发到/error#xff08;默认…一、ERROR Filter
错误过滤器用来处理zuul异常一般使作为打印异常堆栈、跳转异常页面、转换异常信息格式返回等操作。
Zuul已定义的错误过滤器SendErrorFilter如果RequestContext.getThrowable()不为null则转发到/error默认情况下。您可以通过设置error.path属性来更改默认转发路径/error。 二、自定义错误过滤器
本文自定义错误过滤器用来将json格式请求的异常信息转换成json格式返回。 三、实现代码
Component
Slf4j
public class ErrorFilter extends ZuulFilter {//按类型对过滤器进行分类。Zuul中的标准类型是pre用于预路由筛选route用于路由到原点post用于后路由筛选error用于错误处理。//我们还支持静态响应的static类型请参阅StaticResponseFilter。可以通过调用FilterProcessor.runFilters(type)//前置过滤器必须返回errorOverridepublic String filterType() {return FilterConstants.ERROR_TYPE;}//必须为过滤器定义filterOrder。如果优先级对筛选器不重要则过滤器可能具有相同的过滤器顺序//过滤器顺序不需要是连续的Overridepublic int filterOrder() {return FilterConstants.SEND_ERROR_FILTER_ORDER - 10;}//默认情况下zuulfilter是静态的它们不携带状态。这可以通过将isStaticFilter属性重写为false来重写Overridepublic boolean isStaticFilter() {return super.isStaticFilter();}//要禁用此筛选器的Archaius属性的名称。默认情况下它是zuul.[classname].[filtertype].disableOverridepublic String disablePropertyName() {return super.disablePropertyName();}//如果为true则过滤器已被archaius禁用不会运行Overridepublic boolean isFilterDisabled() {return super.isFilterDisabled();}//此方法返回的true表示应该调用run方法//如果应该调用run方法则返回true。false不会调用run方法Overridepublic boolean shouldFilter() {RequestContext ctx RequestContext.getCurrentContext();String contentType ctx.getRequest().getContentType();if (contentType null) {return true;}MediaType mediaType MediaType.valueOf(contentType);if(mediaType null){return true;}return MediaType.APPLICATION_JSON.includes(mediaType);}//如果shouldFilter方法为true则将调用此方法。这种方法是ZuulFilter的核心方法//返回一些可以返回的任意工件。当前的实现忽略了它。//如果在执行期间发生错误则引发ZuulExceptionOverridepublic Object run() throws ZuulException {RequestContext context RequestContext.getCurrentContext();ZuulException exception findZuulException(context.getThrowable());context.remove(throwable);//去掉已处理的错误信息try {HttpServletResponse response context.getResponse();response.setContentType(application/json; charsetutf8);response.setStatus(exception.nStatusCode);PrintWriter writer null;try {writer response.getWriter();MapString, Object map new HashMap();map.put(code, exception.nStatusCode);map.put(msg, exception.errorCause);map.put(detail, exception.getMessage());String retStr JSON.toJSONString(map);writer.print(retStr);writer.flush();} catch (IOException e) {e.printStackTrace();} finally {if (writer ! null) {writer.close();}}}catch (Exception e){log.error(error filter exception, e);}return null;}protected ZuulException findZuulException(Throwable throwable) {if (throwable.getCause() instanceof ZuulRuntimeException) {Throwable cause null;if (throwable.getCause().getCause() ! null) {cause throwable.getCause().getCause().getCause();}if (cause instanceof ClientException cause.getCause() ! null cause.getCause().getCause() instanceof SocketTimeoutException) {ZuulException zuulException new ZuulException(, 504,ZuulException.class.getName() : Hystrix Readed time out);return zuulException;}// this was a failure initiated by one of the local filtersif (throwable.getCause().getCause() instanceof ZuulException) {return (ZuulException) throwable.getCause().getCause();}}if (throwable.getCause() instanceof ZuulException) {// wrapped zuul exceptionreturn (ZuulException) throwable.getCause();}if (throwable instanceof ZuulException) {// exception thrown by zuul lifecyclereturn (ZuulException) throwable;}// fallbackreturn new ZuulException(throwable, HttpStatus.INTERNAL_SERVER_ERROR.value(), null);}
}