使用Spring Boot和Open false将MultipartFile发送到REST API时出错



我试图附加一个文件作为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引导'2.0.2.RELEASE'和:

"io.github.openfeign.form:feign-form:3.4.1",
"io.github.openfeign.form:feign-form-spring:3.4.1",

我检查了这些帖子:

如何通过Spring cloud Feign 发送POST请求

找不到适用于响应类型的HttpMessageConverter

无法写入请求:找不到适用于请求类型和内容类型的HttpMessageConverter

将文件转换为多文件

有什么建议吗?

feign.codec.EncodeException在对消息进行编码时出现问题。我认为@PathVariable('file') MultipartFile multiFile可以转换为base64字符串,并将其传递给REST API或向MultipartFile 添加编码器

最新更新