我在使用假装上传图片时遇到问题。我有使用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
在我的表单中,我的字段包含一个前面的多部分文件
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但是我已经检查了编码器的代码,但是当RequestBody中包含Multipartfile时它似乎不支持。
@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,但我似乎无法使其正常工作。我得到一个错误,它说我传递了多个身体参数。
答案 0 :(得分:1)
也许您应该在映射注释中使用“ consumes”,这对Spring Boot 2和spring-cloud-starter-openfeign来说很有效:
sig.addReference("//samlp:Response");
答案 1 :(得分:0)
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));
}
}
}
答案 2 :(得分:0)