使用Feign调用文件上传
目前feign不支持调用文件上传接口需要自行配置来满足feign的调用方式
maven代码块
12345678910 <dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.2.2</version>
</dependency>
FeignClient接口代码块
1234567891011121314151617181920 url = "${manager.base-gateway}"+"/base-file-mgt", name = "base-file-mgt",configuration = UploadFileService.FeignMultipartSupportConfig.class)
public interface UploadFileService {
(method = RequestMethod.POST, value = "/api/upload-file",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
FileInfoDTO uploadPayImage( (value = "file") MultipartFile file);
class FeignMultipartSupportConfig {
public Encoder multipartFormEncoder() {
return new SpringFormEncoder();
}
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
}
(
- feign.Logger.Level.FULL 日志打印类型
- SpringFormEncoder 字符集转换
打开注解@RequestPart代码
12345678910111213141516171819202122232425262728 ElementType.PARAMETER)
(RetentionPolicy.RUNTIME)
public @interface RequestPart {
/**
* Alias for {@link #name}.
*/
("name")
String value() default "";
/**
* The name of the part in the {@code "multipart/form-data"} request to bind to.
* @since 4.2
*/
("value")
String name() default "";
/**
* Whether the part is required.
* <p>Defaults to {@code true}, leading to an exception being thrown
* if the part is missing in the request. Switch this to
* {@code false} if you prefer a {@code null} value if the part is
* not present in the request.
*/
boolean required() default true;
}
(
@RequestPart 才是支持文件上传的注解。{@code “multipart/form-data”}
FileInfoDTO.java
1234567891011121314151617181920212223242526 public class FileInfoDTO implements Serializable {
private Long id;
private String tenantId;
private String createUserId;
private String filePath;
private String fileId;
private ZonedDateTime createDate;
private Integer disabled;
private String cosPath;
private String fileName;
private ZonedDateTime updateDate;
private String updateUserId;
//get and set
}
###Controller代码块
12345678910 "/orders-pay-img")
(value = "上传订单图片", httpMethod = "POST", notes = "上传订单图片")
public ResponseEntity<FileInfoDTO> createOrder( (value = "file") MultipartFile file,
(name = "filePath", value = "指定文件存储路径(不指定则自动生成文件路径)") (value = "filePath", required = false) String filePath) {
log.debug("REST request to iamge file : {}", file);
FileInfoDTO responseEntity= uploadFileService.uploadPayImage(file,tenantId,createUserId,filePath);
return ResponseUtil.wrapOrNotFound(Optional.ofNullable(responseEntity));
}
(