我正在尝试将文件附加为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()
}
但是我还有另一个例外:
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
有什么建议吗?
答案 0 :(得分:0)
feign.codec.EncodeException
在编码消息时出现问题时引发。
我认为@PathVariable('file') MultipartFile multiFile
可以转换为base64字符串,然后将其传递给REST API或将编码器添加到MultipartFile