java - 如何在 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 结束
解决方案
覆盖过滤器逻辑的 run() 方法
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
...
return null;
}
推荐阅读
- r - RStudio - 选择要片段的代码 - 以编程方式更新 r.snippets
- javascript - 如何在css中的特定时间改变颜色?
- r - 从网络摄像头流中保存帧
- c# - C# 如何使用电子邮件和密码身份验证注册并登录 Firebase?
- wpf - 绑定到 DataGrid 单元格中的 UserControl:BindingExpression 路径错误
- python-3.x - Python Inquirer 模块:完成后删除选择(使用诅咒)
- python-3.x - Discord Bot 不响应命令
- swift - Swift unix 时间戳太短
- html - 我需要放慢我的代码才能在屏幕上看到一些动作
- java - 获取 PDF 中的确切字符串位置,以便稍后用于更改它