2

我在使用 feign 上传图片时遇到问题。我有多个使用 Spring Cloud 的服务。下面是我的依赖项的版本

spring boot - 1.4.3.RELEASE
spring-cloud-starter-feign - 1.1.3.RELEASE
io.github.openfeign.form - 2.2.1
io.github.openfeign.form - 2.2.1

在我的表单中,我有一个 Multipartfile ex 下面的字段

public class MyFrom {
    private String field1;
    private String field2;
    private MultipartFile image;

    //getters and setters
}

并将它传递给我的假客户

@RequestMapping(value = { "/api/some-task},
        method = RequestMethod.POST,
        consumes = {"multipart/form-data"})
ResponseEntity<MyForm> addPromoTask(@RequestBody MyForm request);

我已经在我的代码中添加了一个 SpringFormEncoder,但是我检查了编码器的代码,但是当 Multipartfile 包含在 RequestBody 中时,它似乎不支持。

@FeignClient(value = "some-feign",
    fallback = SomeTaskClient.SomeTaskClienttFallback.class,
    configuration = SomeTaskClient.CoreFeignConfiguration.class)
public interface SomeTaskClient extends SomeTaskApi {

    @Configuration
    class CoreFeignConfiguration {

        @Bean
        @Primary
        @Scope(SCOPE_PROTOTYPE)
        Encoder feignFormEncoder() {
            return new SpringFormEncoder();
        }
    }
}

我已经看到您可以在下面的链接中传递多个@RequestPart,但我似乎无法使其工作。我收到一个错误,它说我正在传递多个正文参数。

https://github.com/bilak/spring-multipart-feign-poc/blob/master/src/main/java/com/github/bilak/poc/ContentClient.java

4

2 回答 2

2

1.需要在pom.xml文件中升级fake form的依赖版本

<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form</artifactId>
   <version>3.0.0</version>
</dependency>
<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form-spring</artifactId>
   <version>3.0.0</version>
</dependency>
  1. 修改配置客户端如下

@FeignClient(name = "service1", configuration = {MultipartSupportConfig.class})
public interface FileUploadServiceClient {
	@RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
	public @ResponseBody String handleFileUpload(
							@RequestPart(value = "file", required = true) MultipartFile file,
	                        @RequestParam(value = "name") String name) throws IOException;

	@Configuration
	public class MultipartSupportConfig {

		@Autowired
		private ObjectFactory<HttpMessageConverters> messageConverters;

		@Bean
		@Primary
		@Scope("prototype")
		public Encoder feignEncoder() {
			return new SpringFormEncoder(new SpringEncoder(messageConverters));
		}
	}
}

问题参考 https://github.com/OpenFeign/feign-form/issues/19

于 2018-11-15T19:11:58.420 回答
2

也许你应该在映射注释中使用“consumes”,这对我来说适用于 spring boot 2 和 spring-cloud-starter-openfeign:

   @PostMapping(value="/upload", consumes = "multipart/form-data" )
   QtiPackageBasicInfo upload(@RequestPart("package") MultipartFile package);
于 2019-11-08T16:18:51.057 回答