当前位置: 首页 > news >正文

前端调用a服务,a服务将请求用controller+openfeign调用b服务,接口参数中有header参数和body,a服务应该怎么设置,才简单

在 Spring Cloud 中使用 OpenFeign 调用服务,处理 header 和 body 参数的简单设置方法:

1. 最简单的配置方式

1.1 定义 Feign 客户端接口

@FeignClient(name = "b-service", url = "${b-service.url}") public interface BServiceClient { @PostMapping("/api/endpoint") ResponseEntity<String> callBService( @RequestHeader Map<String, String> headers, // 接收所有header @RequestBody Object body // 接收请求体 ); // 或者指定特定的header @PostMapping("/api/endpoint") ResponseEntity<String> callBServiceWithSpecificHeaders( @RequestHeader("Authorization") String token, @RequestHeader("X-Request-Id") String requestId, @RequestBody Object body ); }

1.2 在 Controller 中直接传递

@RestController @RequestMapping("/api") public class AController { @Autowired private BServiceClient bServiceClient; @PostMapping("/call-b") public ResponseEntity<?> callBService( HttpServletRequest request, @RequestBody Object requestBody) { // 方法1:传递所有header Map<String, String> headers = new HashMap<>(); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); headers.put(headerName, request.getHeader(headerName)); } return bServiceClient.callBService(headers, requestBody); } }

2. 更优雅的拦截器方式(推荐)

2.1 创建 Feign 拦截器

@Component public class FeignRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate template) { // 从 RequestContextHolder 获取当前请求的上下文 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (attributes != null) { HttpServletRequest request = attributes.getRequest(); // 复制所有header Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); String headerValue = request.getHeader(headerName); template.header(headerName, headerValue); } } // 可以添加公共header template.header("X-Service-Name", "a-service"); } }

2.2 简化的 Feign 客户端

@FeignClient( name = "b-service", url = "${b-service.url}", configuration = FeignConfig.class ) public interface BServiceClient { @PostMapping("/api/endpoint") ResponseEntity<String> callBService(@RequestBody Object body); // 不需要显式传递header,拦截器会自动处理 }

2.3 简化的 Controller

@RestController @RequestMapping("/api") public class AController { @Autowired private BServiceClient bServiceClient; @PostMapping("/call-b") public ResponseEntity<?> callBService(@RequestBody Object requestBody) { // 只需要传body,header会自动传递 return bServiceClient.callBService(requestBody); } }

3. 使用 Feign Builder 简化配置

3.1 配置类

@Configuration public class FeignConfig { @Bean public RequestInterceptor requestInterceptor() { return template -> { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (attributes != null) { HttpServletRequest request = attributes.getRequest(); // 复制特定的重要header String authHeader = request.getHeader("Authorization"); if (authHeader != null) { template.header("Authorization", authHeader); } template.header("X-Request-Id", request.getHeader("X-Request-Id")); template.header("Content-Type", request.getContentType()); } }; } }

4. 全局配置(application.yml)

feign: client: config: default: # 全局默认配置 connectTimeout: 5000 readTimeout: 5000 loggerLevel: basic b-service: # 特定服务配置 connectTimeout: 3000 readTimeout: 10000

5. 最简单的完整示例

// Controller @PostMapping("/simple-call") public ResponseEntity<?> simpleCall( @RequestHeader Map<String, String> headers, @RequestBody Map<String, Object> body) { // 直接转发 return bServiceClient.callBService(headers, body); } // Feign Client @FeignClient(name = "b-service", url = "${b-service.url}") public interface BServiceClient { @PostMapping(value = "/api/process", consumes = "application/json") ResponseEntity<Map<String, Object>> callBService( @RequestHeader Map<String, String> headers, @RequestBody Map<String, Object> body ); }

建议

最简单实用的方案:使用拦截器方式(方案2),原因:

  1. 代码最简洁,Controller 只需要处理业务逻辑

  2. Header 传递对调用方透明

  3. 可以统一处理认证、日志等公共逻辑

  4. 维护性好,修改 header 传递逻辑只需改一处

注意事项

  1. 确保RequestContextHolder在异步调用中可用

  2. 敏感 header 可能需要过滤

  3. 注意 body 对象的序列化/反序列化

  4. 设置合理的超时时间

http://www.jsqmd.com/news/206122/

相关文章:

  • adb wifi连接!
  • HttpServletRequest request获取整个headers有什么方法
  • 硬核盘点:网络安全关键技术栈的高能总结与演进分析
  • 微信小程序开发入门
  • 网络安全技术核心框架:一文理清从原理到实践的知识体系
  • 学网络安全,一张清单就够了!五大核心技术通俗解析与入门路径
  • FeignRequestInterceptor 原理详解
  • 网络安全保姆级入门:手把手详解五大核心技术,小白也能快速上手
  • 信息安全的核心支柱:深入解析网络安全技术体系与实践
  • TOON:一种为大模型设计的JSON压缩型数据结构
  • 《深入剖析 NumPy ndarray:为何比 Python List 快 50~100 倍?》
  • 《深度剖析 Pandas GroupBy:底层实现机制与性能瓶颈全景解析》
  • 百考通AI:源码宝库+智能答辩,毕业季的终极“外挂”
  • 百考通AI:源码宝库+智能答辩,毕业季的终极“外挂”
  • Aurix TC387 Can配置记录
  • 一站式办公平台 vs 单一即时通讯软件:企业该如何抉择?
  • 分享一个MySQL 8.0复制架构主从自动切换脚本
  • 告别论文问卷烦恼!百考通AI带你轻松搞定问卷设计与分析
  • 跨境TRO侵权是什么意思,和解最低金额是多少,如何解决,卖家必须了解的跨境TRO侵权知识
  • 智能体工程全指南:从概念到落地,深度复盘发展现状,收藏这一篇就够了!
  • 深度学习赋能学术写作:百考通AI如何重塑科研全流程体验
  • AI圈炸锅!GraphRAG让大模型不再“一本正经地胡说八道“,小白程序员也能上手的知识图谱增强技术!
  • 避坑!外观专利侵权判定不看百分比!3步实操法+工具实测,跨境卖家/产品人必藏
  • 把 Web App 装进客户端——Tauri框架实战:托盘功能、消息通知、构建安装程序
  • 开题报告智能构建:百考通AI如何重塑论文写作的起点
  • 【程序员必看】RAG技术天花板被打破!AutoRefine让大模型学会“思考式检索“,代码开源,小白也能上手!
  • 【AI黑科技】RAG检索增强生成,让大模型从“一本正经地胡说“到“引经据典“!
  • 学霸同款2026 AI论文写作软件TOP9:自考毕业论文必备测评
  • 书籍-沃尔特·克里斯塔勒《德国南部中心地原理》
  • c语言学习笔记(8)位运算符,++,--运算符的用法