4

我正在尝试附加一个要发送到端点的文件,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",

我检查了这些帖子:

Spring Cloud Feign如何发送POST请求

没有找到适合响应类型的 HttpMessageConverter

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

将文件转换为多部分文件

有什么建议吗?

4

1 回答 1

0

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

于 2018-11-14T13:10:12.883 回答