使用Spring OpenFeign 传输文件

2023-05-16

这里写自定义目录标题

  • B模块上传文件调用了A模块的文件保存实现
    • 1.1 业务场景
    • 2.1 排查过程
    • 3.1 解决办法
    • 4.1 知识扩展
      • 4.1.1 @PostMapping 参数含义
        • 4.1.2 接口参数描述
  • 完整代码
  • 1 引入OpenFeign依赖
  • A模块(提供方)
    • controller层
  • B 模块(调用方)
  • service 层
  • 有问题可以私信我,或者评论, 看到会回复

B模块上传文件调用了A模块的文件保存实现

1.1 业务场景

首先在A模块中写好了上传文件并保存文件的接口与实现,
其次 B模块在开发中,需要调用A模块的接口
A模块接口 使用form-data传输file文件
B模块在使用OpenFeign调用A接口时, 出现异常:

	feign.FeignException: status 400 reading xxx

2.1 排查过程

A 模块接口正常
B 模块使用form-data接受正常
B 模块调用A模块接口,传参时异常, 因为调用OpenFeign时没有使用form-data

3.1 解决办法

方法: B 模块 在使用OpenFeign 调用A模块接口时, 需要 如下改动

  1. 在@PostMapping() 指定consumes (详细含义,看4.1章节)
  2. file 使用@RequestPart 注解
@ApiOperation(value = "添加拓扑图CAD设计图纸")
@PostMapping(path = "/design/file/target/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
DesignFile addDesignFile(@PathVariable("id") Integer targetId,
                         @RequestParam(value = "targetType") String targetType,
                         @RequestPart("file") MultipartFile file);

该代码中有需要注意:
必须是Post类型的请求。
文件形参必须用@RequestPara MultipartFile file来修饰,其中 @RequestParat和MultipartFile必不可少。

4.1 知识扩展

4.1.1 @PostMapping 参数含义

@PostMapping是专门描述post请求的注解
@PostMapping等同于@RequestMapping(method = RequestMethod.POST)

参数含义
path请求路径
consumes请求提交的内容格式
method请求类型

4.1.2 接口参数描述

参数含义
@PathVariable(“参数名”) 类型 变量名参数在url中写, URL占位中的参数
@RequestParam(value=“参数名”) 类型 变量名参数在body中写,是URL中?后面的参数
@RequestPart(“参数名”) MultipartFile 变量名特指文件传递时使用,是修改文件的参数

完整代码

1 引入OpenFeign依赖

 <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>xxx</version>
        </dependency>

        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>xxx</version>
        </dependency>

A模块(提供方)

controller层

@RestController
public class DesignFileController {
	@SneakyThrows
	@ApiOperation(value = "添加拓扑图CAD设计图纸")
	@PostMapping("/design/file/target/{id}")
	public DesignFile addDesignFile(@PathVariable("id") Integer targetId,
	                                @RequestParam(value = "targetType") String targetType,
	                                @RequestParam("file") MultipartFile file) {
	    val fileName = file.getOriginalFilename();
	   return  this.designFileService.addDesignFile(targetId, fileName, description, targetType);
	}
}

B 模块(调用方)

service 层

/**
 * @Author: yibo
 * @Date: 2020-03-17 11:30
 * @Description: 拓扑图附件
 */
 @FeignClient(
        name = "multipart-support-service",
        url = "http://localhost:8080",
        configuration = Client.ClientConfiguration.class
)
public interface GraphService {
    @ApiOperation(value = "添加拓扑图CAD设计图纸")
    @PostMapping(path = "/design/file/target/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    DesignFile addDesignFile(@PathVariable("id") Integer targetId,
                             @RequestParam(value = "targetType") String targetType,
                             @RequestParam(value = "description", required = false) String description,
                             @RequestPart("file") MultipartFile file);
}

有问题可以私信我,或者评论, 看到会回复

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用Spring OpenFeign 传输文件 的相关文章

随机推荐

  • Android Studio Kotlin插件的简单使用

    1 新建一个Project Kotlin xff0c 然后在app的build gradle文件中添加配置 xff0c 如图所示 xff1a 2 在main下新建一个 39 kotlin 39 文件夹 xff0c 然后添加配置 xff0c
  • Android Studio Java文件和Kotlin文件的转换

    打开Java文件 xff0c 然后选择工具栏的 Code gt Convert Java File to Kotlin File
  • 解决NestedScrollView 嵌套 RecyclerView出现的卡顿,上拉刷新无效

    解决卡顿的方法最简单的就是设置RecyclerView 的android nestedScrollingEnabled 61 34 false 34 xff0c 放弃自己的滑动 交给外部的NestedScrollView处理 就没有出现卡顿
  • 获取系统当前时间,Time过时

    用Time获取系统当前时间时会出现警告 xff1a The type Time is deprecated xff0c The method setToNow from the type Time is deprecated xff0c T
  • AndroidStudio 编译异常java.lang.OutOfMemoryError: GC overhead limit exceeded

    在build gradle中的android 添加如下脚本就可以顺利编译了 dexOptions incremental true javaMaxHeapSize 4g 参考 xff1a http blog csdn net ydt lwj
  • Mac上使用Royal TSX链接服务器

    在Windows上好用的shell工具可能要数xshell xff0c 但xshell并没有开发mac版本 xff0c 所以想和远程服务器SSH连接可能要用macOS自带的终端terminal或者iTerm2 试过过几个工具软件后 xff0
  • libevent详解四(http服务)

    创建基于libevent的http服务 先上代码 span class token macro property span class token directive keyword include span span class toke
  • 生产者消费者模式解决API响应速度不一致的问题

    问题引入 项目里需要用到 问题生成大模型来生成问题 xff0c 所以采用了FastAPI部署 xff0c 然后对外提供API从而在后端调用 xff0c 模型一次只能对一个文本进行问题生成 xff0c 然而每次后端都会收到前端传来的文件 xf
  • 如何在用户目录下建立.kube文件夹

    文章目录 步骤新建文件夹如果直接输入 文件名 系统提示错误我们要输入 文件名 成功 步骤 在指定的路径下 新建一个文件夹输入 文件名 完成 新建文件夹 在指定的路径下 新建一个文件夹 如果直接输入 文件名 系统提示错误 我们要输入 文件名
  • 面向对象设计 个人汇总

    文章目录 七大原则三大特性 七大原则 开 面向扩展开放 面向修改关闭口 接口隔离原则合 组合 聚合原则里 里氏替换原则最 最少知识原则单 单一责任原则依 依赖倒置原则 三大特性 封装继承多态
  • idea单词检测出错怎么办,自定义添加单词

    完成
  • Spring Boot 2下导入依赖后使用Feign找不到@EnableFeignClients的解决办法

    文章目录 解决方案详细解决步骤1 导入依赖2 引用注解3 然后看一下maven是否引好4 在pom依赖中加上代码5 maven看一下是否成功6 使用注解 完结撒花 解决方案 pom文件中这样引用 lt feign依赖 gt lt depen
  • idea隐藏资源栏目下的文件显示

    在资源栏目下忽略这两个文件 widowS系统 步骤 MAC系统
  • swagger依赖的选用

    总结 如果你的项目使用jax rs来实现RESTful接口 xff0c 你就用io swagger来集成swagger xff1b 如果你的项目使用springmvc来实现RESTful接口 xff0c 最新的方法还是推荐使用springf
  • 谷歌chrome的页面保持为图片

  • 什么是中间件

    中间件 xff0c 是位于操作系统和应用程序中间的组件 为应用程序提供运行环境和管理支撑 常见的中间件 xff0c 商业的有weblogic WebSphere 用友 金蝶的 xff0c xff0c 开源的jboss tomcat也算中间件
  • Spring框架的7大功能

    目录 Spring框架的7大功能核心容器 Spring core Spring面向切面编程 Spring AOP Spring ORM模块Spring DAO模块Spring Web模块Spring Web模块Spring MVC框架 Sp
  • Stream流 初级使用

    文章目录 Stream流提供更好操作的集合库初始操作stream stream流parallelStream 多核流Map进行流操作 中间操作filter 筛选map 提取limit 截断sorted 排序distinct 去重 终端操作f
  • SpringMVC笔记

    1 SpringMVC概述 1 1 SpringMVC概念 SpringMVC也叫Spring web mvc 是Spring内置的一个MVC框架 xff0c 在Spring3 0后发布 SpringMVC框架解决了WEB开发中常见的问题
  • 使用Spring OpenFeign 传输文件

    这里写自定义目录标题 B模块上传文件调用了A模块的文件保存实现1 1 业务场景2 1 排查过程3 1 解决办法4 1 知识扩展4 1 1 64 PostMapping 参数含义4 1 2 接口参数描述 完整代码1 引入OpenFeign依赖