Spring boot openfeignクライアントを使用して画像をアップロードするには、Feignがサービス間で画像を転送します.

1772 ワード

シーン:
画像をアップロードするサービスはAであり、Bサービスはfeignクライアントを介してAサービスを呼び出し、MultipartFileを伝達する.
手順1:依存関係の追加
        
		
			io.github.openfeign.form
			feign-form
			3.8.0
		
		
			io.github.openfeign.form
			feign-form-spring
			3.8.0
		


ステップ2:構成FeignConfigurationの追加
@Configuration
public class FeignConfiguration implements RequestInterceptor {
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    @Autowired
    private ObjectFactory messageConverters;
    // new  form   ,    form    

    @Bean
    public Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }
}

手順3:Feignクライアント調整
@PostMapping(value = "/fileupload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String fileUpload(@RequestPart(value = "file") MultipartFile file, @RequestParam String path);

注意:@RequestParam (value = "file")は使用できません.@RequestPart(value = "file")は使用できません.
よくある異常
feign.codec.EncodeException: Could not write request: no suitable HttpMessageConverter found for request type
Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found