尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

@FeignClient调用Get请求传递对象参数、经验总结

@FeignClient调用Get请求传递对象参数、经验总结 一、引入 openfeign 依赖!-- openfeign -- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-openfeign/artifactId /dependency !-- 使用 openfeign 自家的 feign httpclient 代替 Apache httpclient 发送请求 -- dependency groupIdio.github.openfeign/groupId artifactIdfeign-httpclient/artifactId version10.1.0/version /dependency二、调用方 FeignClient 端代码示例FeignClient(name payment-service, path payment-service, contextId PaymentServerClient) public interface PaymentFlowClient { Operation(summary 查询支付流水信息列表-根据对象类型入参) GetMapping(value /paymentFlow/getPaymentFlowListByObj) ResListPaymentFlowRes getPaymentFlowListByObj(SpringQueryMap GetPaymentFlowListReq data); Operation(summary 查询支付流水信息列表-根据基本类型入参) GetMapping(value /paymentFlow/getPaymentFlowListByBaseType) ResListPaymentFlowRes getPaymentFlowListByBaseType(RequestParam(required false) String flowNo, RequestParam(required false) String orderNo, RequestParam(required false) String remark); }三、被调用方的 Controller 层接口示例Operation(summary 查询支付流水信息列表-根据对象类型入参) GetMapping(value /getPaymentFlowListByObj) public ResListPaymentFlowRes getPaymentFlowListByObj(ParameterObject GetPaymentFlowListReq data) { return Res.ok(paymentFlowService.getPaymentFlowList(data)); } Operation(summary 查询支付流水信息列表-根据基本类型入参) GetMapping(value /getPaymentFlowListByBaseType) public ResListPaymentFlowRes getPaymentFlowListByBaseType(RequestParam(required false) String flowNo, RequestParam(required false) String orderNo, RequestParam(required false) String remark ) { ListPaymentFlowRes paymentFlowList paymentFlowService.getPaymentFlowList(GetPaymentFlowListReq.builder().flowNo(flowNo).orderNo(orderNo).remark(remark).build()); return Res.ok(paymentFlowList); }说明1、get 请求方式如果传递的参数是对象类型则 FeignClient 端的接口对象类型参数则需要加SpringQueryMap注解被调用方 controller 接口则按正常的 rest api标准写就可以2、如果传递的是基本类型的参数则 FeignClient 端的接口方法参数 和 被调用方Controller接口方法参数(包括注解)一致就可以了3、如果是 post 请求方式可能会报请求超时的错误因为 OpenFeign 的默认请求连接时间仅有几秒钟需要把请求时间配置的更长一些参考如下配置https://blog.csdn.net/hkl_Forever/article/details/120291464总结1、通过 FeignClient 调用可以把需要暴露的api放在本项目的api模块中映射地址指向本项目业务模块的controller映射地址即可其他服务只需依赖本项目jar包即可直接注入调用推荐此方式2、FeignClient 代理接口类中的方法一般最好与被调用服务中Controller类中的映射方法一致
返回列表