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

9 浏览
0 Comments

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

我试图将一个文件作为MultipartFile附加到发送给端点的请求中,但是我得到了以下异常:\n

期望没有异常被抛出,但得到了 'feign.codec.EncodeException'
//...
原因:feign.codec.EncodeException: 无法写入请求:
找不到适合的HttpMessageConverter来处理请求类型 [java.util.LinkedHashMap] 和内容类型 [multipart/form-data]

\n我的方法是:\n

//...
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
}

\n我的接口是:\n

@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)
}

\n我的配置文件是:\n

@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()
                }
            }
        }
    }
}

\n此外,我尝试将以下代码添加到我的配置文件中:\n

@Bean
Encoder encoder() {
    return new FormEncoder()
}

\n但是我又遇到了另一个异常:\n

无法将对象 'feign.form.FormEncoder@5fa78e0a' 
从类 'feign.form.FormEncoder' 转换为类 'java.beans.Encoder'

\n我正在使用 Spring Boot \'2.0.2.RELEASE\' 和以下依赖:\n

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

\n我已经查看了以下帖子:\nHow to send POST request by Spring cloud Feign\nno suitable HttpMessageConverter found for response type\nCould not write request: no suitable HttpMessageConverter found for request type and content type\nConverting file to multipartfile\n有什么建议吗?

0
0 Comments

问题原因:在使用Spring Boot和Open feign发送MultipartFile到REST API时,出现了feign.codec.EncodeException的错误。可能是因为('file') MultipartFile multiFile无法正确地被编码为消息,导致出错。

解决方法:我认为可以将('file') MultipartFile multiFile转换为base64字符串,并将其传递给REST API,或者为MultipartFile添加一个编码器。由于API只接受Content-Type: multipart/form-data; boundary="the boundary here",所以问题可能是如何正确使用编码器。

在使用Spring Boot和Open feign发送MultipartFile到REST API时,可能会遇到feign.codec.EncodeException的错误。这个错误通常在编码消息时出现问题。

当我们尝试将('file') MultipartFile multiFile发送到API时,它可能无法正确地被编码为消息。为了解决这个问题,我们可以尝试将MultipartFile转换为base64字符串,并将其作为参数传递给API。这样可以确保数据正确地被编码和传输。

另一种解决方法是为MultipartFile添加一个编码器。通过使用编码器,我们可以自定义如何将MultipartFile编码为消息。这样,我们可以确保消息的格式符合API所要求的Content-Type: multipart/form-data; boundary="the boundary here"。

然而,这个错误可能还是由于其他原因造成的。在上述讨论中,某些情况下这个问题的解决方法,但是没有找到确切的解决方案。因此,我们可能需要进一步研究并尝试不同的解决方法来解决这个问题。

如果你已经找到了解决方法,请在这里发布。否则,我们需要继续寻找解决方案。

0