我在使用假装上传图像时遇到问题。我有多个使用弹簧云的服务。我的依赖项版本如下
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
在我的表格中,我有下面带有多部分文件的字段
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
1.您需要升级pom.xml文件中假装形式的依赖版本
<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>
- 按如下方式修改配置客户端
@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
也许你应该在映射注释中使用"消耗",这对我适用于 Spring boot 2 和 spring-cloud-starter-openfeign:
@PostMapping(value="/upload", consumes = "multipart/form-data" )
QtiPackageBasicInfo upload(@RequestPart("package") MultipartFile package);