我试图附加一个文件作为 MultipartFile
发送到端点但我收到此异常:
Expected no exception to be thrown, but got 'feign.codec.EncodeException'
//...
Caused by: feign.codec.EncodeException: Could not write request:
no suitable HttpMessageConverter found for request type [java.util.LinkedHashMap]
and content type [multipart/form-data]
我的方法是:
//...
final User user
//...
@Override
DocumentResponse attachDocument(File file, String userId, String documentId) {
String timestamp = String.valueOf(System.currentTimeMillis())
String url = "${myProperties.apiUrl}/documents/attach?ts=${timestamp}"
String digest = myJWT.sign(HttpMethod.POST, url)
MultipartFile multiFile = new MockMultipartFile("test.xml",
new FileInputStream(file))
DocumentResponse documentResponse = user.attachDocument(multiFile,
userId, documentId, timestamp, digest)
return documentResponse
}
我的界面是:
@FeignClient(name = 'myUser', url = '${apiUrl}', configuration = myConfiguration)
interface User {
//...
@PostMapping(value = '/documents/attach', consumes = 'multipart/form-data')
DocumentResponse attachDocument(@PathVariable('file') MultipartFile multiFile,
@PathVariable('clientId') String userId,
@PathVariable('documentId') String documentId,
@RequestParam('ts') String timestamp,
@RequestParam('digest') String digest)
}
我的配置文件是:
@Slf4j
@Configuration
class myConfiguration {
@Bean
Retryer feignRetryer(@Value('${feign.client.config.myUser.period}') Long period,
@Value('${feign.client.config.myUser.maxInterval}') Long maxInterval,
@Value('${feign.client.config.myUser.maxAttempts}') Integer maxAttempts) {
return new Retryer.Default(period, maxInterval, maxAttempts)
}
@Bean
ErrorDecoder errorDecoder() {
return new ErrorDecoder() {
@Override
Exception decode(String methodKey, Response response) {
if (HttpStatus.OK.value() != response.status()) {
FeignException ex = FeignException.errorStatus(methodKey, response)
if (response.status() != HttpStatus.BAD_REQUEST.value()) {
return new RetryableException('getting conflict and retry', new Date(System.currentTimeMillis() + TimeUnit.SECONDS
.toMillis(1)))
}
return new MyDocumentException()
}
}
}
}
}
另外,我尝试将此代码添加到 myConfiguration 文件中:
@Bean
Encoder encoder() {
return new FormEncoder()
}
但我还有一个异常(exception):
Cannot cast object 'feign.form.FormEncoder@5fa78e0a'
with class 'feign.form.FormEncoder' to class 'java.beans.Encoder'
我正在使用 Spring Boot
'2.0.2.RELEASE'
和:"io.github.openfeign.form:feign-form:3.4.1",
"io.github.openfeign.form:feign-form-spring:3.4.1",
我检查了这些帖子:
How to send POST request by Spring cloud Feign
no suitable HttpMessageConverter found for response type
Could not write request: no suitable HttpMessageConverter found for request type and content type
Converting file to multipartfile
有什么建议吗?
最佳答案
feign.codec.EncodeException
当编码消息出现问题时引发。
我认为@PathVariable('file') MultipartFile multiFile
, 可以转换为 base64 字符串并将其传递给 REST API 或将编码器添加到 MultipartFile
关于java - 使用 Spring Boot 和 Open feign 将 MultipartFile 发送到 REST API 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53299771/