我在使用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,但是我检查了编码器的代码,但是它似乎不支持在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,但我似乎不能让它工作。我得到一个错误,它告诉我我正在传递多个body参数。
发布于 2018-11-16 03:11:58
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));
}
}
}
发布于 2019-11-09 00:18:51
也许你应该在映射中使用'consumes‘,这对我的spring boot 2和spring-cloud-starter-openfeign很有效:
@PostMapping(value="/upload", consumes = "multipart/form-data" )
QtiPackageBasicInfo upload(@RequestPart("package") MultipartFile package);https://stackoverflow.com/questions/47048150
复制相似问题