使用ZuulFilter过滤时候 , 过滤Exception有问题 . 求解答

使用Zuulfilter过滤请求 , filterType()设置为"pre"和"post"都没问题 , 但是设置为"error"
后, 传入错误类型的参数 . 可以看到抛出异常 , 却看不到run()方法的输出 . 代码如下,

 package com.example.filter;

import com.netflix.zuul.ZuulFilter;
import org.apache.log4j.Logger;

public class ErrorFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "error";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        Logger logger = Logger.getLogger(getClass());
        logger.info("errorfilter");

        return null;
    }
}


package com.example;

import com.example.filter.ErrorFilter;
import com.example.filter.PostFilter;
import com.example.filter.PreFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;

@SpringCloudApplication
@EnableZuulProxy
public class Application {
    public static void main(String [] args){
        SpringApplication.run(Application.class,args);
    }

    @Bean
    public ErrorFilter getErrorFilter(){
        return new ErrorFilter();
    }

    @Bean
    public PreFilter getPreFilter(){
        return new PreFilter();
    }

    @Bean
    public PostFilter getPostFilter(){
        return new PostFilter();
    }


}


 2016-10-28 18:33:38.182  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PreFilter             : url : http://localhost:1124/filter/right
2016-10-28 18:33:38.183  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PreFilter             : ip : fe80:0:0:0:cc74:43dd:5bf8:d0fd%41
2016-10-28 18:33:38.183  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PreFilter             : 端口 : 56282
2016-10-28 18:33:38.183  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PreFilter             : 请求方式 : GET
2016-10-28 18:33:38.183  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PreFilter             : 请求参数 : b:[a]
2016-10-28 18:33:38.183  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PreFilter             : 请求参数 : a:[1]
2016-10-28 18:33:38.183  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PreFilter             : 所有消息头 : {connection=keep-alive, accept-language=zh-CN,zh;q=0.8, host=nakupenda:1124, accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8, user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/5.7.16173.12 Safari/537.36, accept-encoding=gzip, deflate, upgrade-insecure-requests=1}
2016-10-28 18:33:38.184  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PreFilter             :           : null
2016-10-28 18:33:38.264  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PostFilter            : url : http://localhost:1124/filter/right
2016-10-28 18:33:38.264  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PostFilter            : ip : fe80:0:0:0:cc74:43dd:5bf8:d0fd%41
2016-10-28 18:33:38.264  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PostFilter            : 端口 : 56282
2016-10-28 18:33:38.264  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PostFilter            : 请求方式 : GET
2016-10-28 18:33:38.264  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PostFilter            : 请求参数 : b:[a]
2016-10-28 18:33:38.264  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PostFilter            : 请求参数 : a:[1]
2016-10-28 18:33:38.264  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PostFilter            : 所有消息头 : {connection=keep-alive, accept-language=zh-CN,zh;q=0.8, host=nakupenda:1124, accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8, user-agent=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/5.7.16173.12 Safari/537.36, accept-encoding=gzip, deflate, upgrade-insecure-requests=1}
2016-10-28 18:33:38.265  INFO 4976 --- [nio-1124-exec-5] com.example.filter.PostFilter            :           : null
  ........null:null
2016-10-28 18:33:45.170  INFO 4976 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2016-10-28 18:38:45.171  INFO 4976 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration




求解答

2个回答

Gang_Z
TFFTF 我问的是zuulFilter实现 , 不是Spring 中的Filter 过滤器
接近 3 年之前 回复

在其他filter中throw exception后就可以被ErrorFilter捕获到了。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!