我正在尝试使用Zuul
将调用重定向到其他地方的下游系统。
在重定向中,我需要添加一个包含必要数据的标头api
接收重定向以进行处理。我似乎无法让下游系统检测到这些数据。附件是我的代码。
我在用Zuul
from Edgware.SR3, Spring Boot 1.5.12
Zuul过滤器
@Component
public class RouteFilter extends ZuulFilter{
@Override
public Object run() {
//Testing to add header
context.getRequest().getParameterMap().put("api", new String[]{"api"});
context.getResponse().setHeader("api", api);
context.addZuulResponseHeader("api", "api");
context.addZuulRequestHeader("api", "api");
context.setSendZuulResponse(false);
context.put(FORWARD_TO_KEY, redirect_urls.get(key));
context.setResponseStatusCode(HttpStatus.SC_TEMPORARY_REDIRECT);
context.getResponse().sendRedirect(redirect_urls.get(key));
return null;
}
}
重定向服务代码
@RequestMapping(value = "/forward")
public ResponseEntity<String> forwardToMe(@RequestHeader(required = true, name = "api")String api){
return new ResponseEntity<String>("Hi",HttpStatus.OK);
}
邮递员收到错误
{
“时间戳”:1524737817729,
“状态”:400,
"error": "错误的请求",
"异常": "org.springframework.web.bind.ServletRequestBindingException",
"message": "字符串类型的方法参数缺少请求标头 'api'",
“前进的道路” }
我猜你用的是路由过滤器,也许你可以尝试使用前置过滤器.
添加自定义标头可以通过以下方式完成:context.addZuulRequestHeader("Authorization", "Basic " + credentials);
.
对于重定向部分,你可以检查这个thread
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)