首页 > 解决方案 > 如何在 Zuul 过滤器中发送 RestTemplate/Request?

问题描述

我有一个核心 MS,它只是一个传递,并向所有传入请求添加标头。我试图在 Zuul 过滤器中调用 Validate Session API,以便在添加所有请求的标头之前先调用它,如果出现此错误,则事务将不会继续。但是,似乎我无法在过滤器内发送基本的休息模板请求。那我做错了什么还是我错过了什么?这是代码片段。

public class WebFilter extends ZuulFilter {

 @Override
    public String filterType() {

        RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
        restTemplate.getForEntity(this.loginService.validateCache(hostUrl,validateSessionUrl, ctx.getRequest().getParameter("SessionId")).toUriString(),String.class

         ctx.addZuulRequestHeader("clientid", clientId);
        ctx.addZuulRequestHeader("clientsecret", clientSecret);
        return "pre";
    }
}

而resttemplate部分给出了ff。错误。但是,删除它将使应用程序正常工作,但是我需要该休息模板来调用验证 API。我应该把它放在别的地方,还是别的什么地方?

2019-12-20 10:11:50.337 错误 4224 --- [main] osboot.SpringApplication:应用程序启动失败

org.springframework.context.ApplicationContextException:无法启动嵌入式容器;嵌套异常是 org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.4.0 .RELEASE.jar:1.4.0.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:535) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] 在 org.springframework.boot.SpringApplication。刷新(SpringApplication.java:759)~[spring-boot-1.4. createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:164) ~[spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:134) ~ [spring-boot-1.4.0.RELEASE.jar:1.4.0.RELEASE] ...省略了8个常见框架原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'org.springframework.cloud的bean时出错.netflix.zuul.ZuulConfiguration$ZuulFilterConfiguration':通过字段“过滤器”表达的不满足的依赖关系:创建 com.bdo.fintech.web.core.Application 中定义的名称为“webFilter”的 bean 时出错:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.bdo.fintech.web.core.WebFilter]:工厂方法“webFilter”抛出异常;嵌套异常是 java.lang.NullPointerException;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建 com.bdo.fintech.web.core.Application 中定义的名称为“webFilter”的 bean 时出错:通过工厂方法进行 Bean 实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.bdo.fintech.web.core.WebFilter]:工厂方法“webFilter”抛出异常;嵌套异常是 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569) 处的 java.lang.NullPointerException ~[spring-beans-4.3.2.RELEASE.jar:4.3.2。通过工厂方法实例化 Bean 失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [com.bdo.fintech.web.core.WebFilter]:工厂方法“webFilter”抛出异常;嵌套异常是 java.lang.NullPointerException 在 org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.beans.factory.support .AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018) ~[spring-beans-4.3.2. doResolveDependency(DefaultListableBeanFactory.java:1049) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~ [spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566) ~[spring-beans-4.3. 2.RELEASE.jar:4.3.2.RELEASE] ...省略了35个常见框架原因:org.springframework.beans.BeanInstantiationException:无法实例化[com.bdo.fintech.web.core.WebFilter]:工厂方法' webFilter' 抛出异常;嵌套异常是 org.springframework.beans.factory.support.SimpleInstantiationStrategy 中的 java.lang.NullPointerException。

进程以退出代码 1 结束

标签: javaspringspring-bootnetflix-zuul

解决方案


覆盖过滤器逻辑的 run() 方法

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

...

        return null;
    }

https://cloud.spring.io/spring-cloud-netflix/multi/multi__router_and_filter_zuul.html#zuul-developer-guide-sample-pre-filter


推荐阅读