Feign調(diào)用傳輸文件異常的解決
1. Current request is not a multipart request
feign接口參數(shù)使用 @RequestPart 而非 @RequestParam, 同時需要指定consumes,比如這樣:
@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
Result<FileStorage> upload(@RequestPart(value = "file") MultipartFile file);
2. Feign failed and no fallback
這是hystrix導(dǎo)致,關(guān)閉feign熔斷,或者延長熔斷的超時時間,我簡單粗暴的直接關(guān)了
3.Read timed out executing POST for “xxx”
配置了hystrix還不行,或者延長ribbon的超時時間,參考了Feign超時問題的辦法,簡單來說就是feign經(jīng)過了ribbonn和hystrix兩級調(diào)用,而且都有一個默認(rèn)的超時時間,延長超時時間就好了
spring:
servlet:
context-path: /farm
application:
name: farm
profiles:
active: dev
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:7001/eureka
instance:
prefer-ip-address: true
#關(guān)閉feign熔斷
feign:
hystrix:
enabled: false
#開啟熔斷,關(guān)閉熔斷超時或延長調(diào)用超時時間
#hystrix:
# command:
# default:
# execution:
# timeout:
# enabled: false
# isolation:
# thread:
# timeoutInMilliseconds: 30000
#延長ribbon超時時間
ribbon:
ReadTimeout: 30000
ConnectTimeout: 30000
通過Feign上傳文件(踩坑)
引入依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
服務(wù)提供者:
@RestController
@RequestMapping("/file")
public interface FileUploadService {
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
CommonResult<String> uploadFile(@RequestPart("file") MultipartFile file,
@RequestParam(value = "containerName", required = false) String containerName
}
具體實現(xiàn)不是重點……根據(jù)你的實際情況去完成……
服務(wù)調(diào)用者:
@RestController
@FeignClient(value = "XXXXXXXX", configuration = FileUploadServiceFeign.ClientConfiguration.class)
@RequestMapping("/file")
public interface FileUploadServiceFeign extends FileUploadService {
/**
* 配置類
*/
class ClientConfiguration {
/**
* 此處注入的是: ObjectFactory<HttpMessageConverters>
*/
@Autowired
private ObjectFactory<HttpMessageConverters> messageConverters;
@Bean
public Encoder feignEncoder() {
return new SpringFormEncoder(new SpringEncoder(messageConverters));
}
}
}
這樣就行了……
需要注意的是:
在服務(wù)調(diào)用者那層的MultipartFile的value要跟服務(wù)提供者的@RequestPart中的value值一樣。不然它會拋出400異常?。?!

成功案例:


以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot中快速操作Mongodb數(shù)據(jù)庫指南
這篇文章主要給大家介紹了關(guān)于Spring Boot中如何快速操作Mongodb的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Springboot swagger配置過程詳解(idea社區(qū)版2023.1.4+apache-maven-3
這篇文章主要介紹了Springboot-swagger配置(idea社區(qū)版2023.1.4+apache-maven-3.9.3-bin),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
jsp+servlet實現(xiàn)簡單登錄頁面功能(附demo)
本文主要介紹了jsp+servlet實現(xiàn)簡單登錄頁面功能登錄成功跳轉(zhuǎn)新頁面,登錄失敗在原登錄界面提示登錄失敗信息,對初學(xué)者有一定的幫助,感興趣的可以了解一下2021-07-07

