Feignを使ってファイルのアップロードを呼び出します.

75711 ワード

Feignを使ってファイルのアップロードを呼び出します.
現在のfeignはファイルアップロードインターフェースの呼び出しをサポートしていません.自分で設定してfeignの呼び出しを満たす必要があります.
mavenコードブロック
12345678910  <dependency>    
    <groupId>io.github.openfeign.formgroupId>    
    <artifactId>feign-form-springartifactId>    
    <version>3.2.2version>    
  dependency>    
  <dependency>    
      <groupId>io.github.openfeign.formgroupId>    
      <artifactId>feign-formartifactId>    
      <version>3.2.2version>    
  dependency>  
Feign Clientインターフェースコードブロック
1234567891011121314151617181920  @FeignClient(url = "${manager.base-gateway}"+"/base-file-mgt", name = "base-file-mgt",configuration = UploadFileService.FeignMultipartSupportConfig.class)
  public interface UploadFileService {
  
    @RequestMapping(method = RequestMethod.POST, value = "/api/upload-file",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    FileInfoDTO uploadPayImage(@RequestPart(value = "file") MultipartFile file);
    
      class FeignMultipartSupportConfig {
    
          @Bean
          public Encoder multipartFormEncoder() {
              return new SpringFormEncoder();
          }
    
          @Bean
          public feign.Logger.Level multipartLoggerLevel() {
              return feign.Logger.Level.FULL;
          }
      }
  }
feign.ロギング.Level.FLLログ印刷タイプSpring FormEncoder文字セット変換コメントを開く@Request Partコード
12345678910111213141516171819202122232425262728  @Target(ElementType.PARAMETER)
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  public @interface RequestPart {

    /**
     * Alias for {@link #name}.
     */
    @AliasFor("name")
    String value() default "";
  
    /**
     * The name of the part in the {@code "multipart/form-data"} request to bind to.
     * @since 4.2
     */
    @AliasFor("value")
    String name() default "";
  
    /**
     * Whether the part is required.
     * 

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; }
@Request Partはファイルアップロードをサポートするコメントです.{code「multiipad/form-data」}
FileInfoDT O.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  @PostMapping("/orders-pay-img")
    @Timed
    @ApiOperation(value = "      ", httpMethod = "POST", notes = "      ")
    public ResponseEntity<FileInfoDTO> createOrder(@RequestParam(value = "file") MultipartFile file,
                                         @ApiParam(name = "filePath", value = "        (            )") @RequestParam(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));
    }