spring cloud feign 调用微服务转发不了request请求头和参数问题解决方案
2017-12-18 17:01
1166 查看
spring cloud feign 调用微服务转发不了request请求头和参数问题解决方案
问题:开发中发现feign调用的微服务获取不到参数,查看文档吧一天终于扒到了解决方案;原理:为feign添加拦截,修改默认config,为feign请求添加原request的请求头以及参数!相关代码如下:
代码块
@Configuration public class FeignConfiguration { private Logger logger = LoggerFactory.getLogger(FeignConfiguration.class); @Bean public RequestInterceptor requestInterceptor() { return new RequestInterceptor() { @Override public void apply(RequestTemplate template) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); template.header(name, values); } } Enumeration<String> bodyNames = request.getParameterNames(); StringBuffer body =new StringBuffer(); if (bodyNames != null) { while (bodyNames.hasMoreElements()) { String name = bodyNames.nextElement(); String values = request.getParameter(name); body.append(name).append("=").append(values).append("&"); } } if(body.length()!=0) { body.deleteCharAt(body.length()-1); template.body(body.toString()); logger.info("feign interceptor body:{}",body.toString()); } } }; } }
相关文章推荐
- springcloud使用feign实现服务间条用,参数数据太大,无法调用成功解决方法
- SpringCloud服务消费者第一次调用出现超时问题的解决方案
- Spring Cloud ZooKeeper集成Feign的坑2,服务调用了一次后第二次调用就变成了500,错误:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.n
- 关于Dalston.SR3版本的spring cloud,用feign客户端调用服务hystrix不能生效的问题
- spring cloud feign 调用服务注意问题
- spring cloud eureka服务调用出现feign.codec.EncodeException: Could not write request: no suitable HttpMessa
- 微服务开发架构——Spring Cloud常见问题与总结<二>Hystrix/Feign 整合Hystrix后首次请求失败
- 使用Spring cloud Feign在后台服务之间调用传递Multipart无法传递的问题
- SpringCloud之声明式服务调用Spring Cloud Feign实例
- Spring Cloud中,如何解决Feign/Ribbon第一次请求失败的问题?
- Spring FeignClient 遇到的参数问题(RequestParam.value() was empty on parameter 0)
- spring cloud 之 Feign 使用HTTP请求远程服务
- spring cloud feign实现远程调用服务传输文件的方法
- 【图文经典版】声明式调用服务SpringCloud之Feign实例讲解
- 笔记:Spring Cloud Feign 声明式服务调用
- SpringCloud使用Feign进行服务调用
- Spring Cloud(十一)声名式服务调用:Feign的使用 (上)
- 快速解决SpringMVC @RequestBody 用map接收请求参数的问题
- 使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务
- Spring Cloud Feign作为HTTP客户端调用远程HTTP服务,feign熔断器