如何在不获取“找不到合适的写入器异常”的情况下将异步数据写入远程端点?

时间:2019-05-02 11:19:30

标签: spring-boot spring-webflux

我有以下控制器方法:

@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE, path = "/upload")
public Mono<SomeResponse> saveEnhanced(@RequestPart("file") Mono<FilePart> file) {
    return documentService.save(file);
}

调用一种服务方法,在该方法中,我尝试使用WebClient将数据放入另一个应用程序:

public Mono<SomeResponse> save(Mono<FilePart> file) {
    MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
    bodyBuilder.asyncPart("file", file, FilePart.class);
    bodyBuilder.part("identifiers", "some static content");
    return WebClient.create("some-url").put()
            .uri("/remote-path")
            .syncBody(bodyBuilder.build())
            .retrieve()
            .bodyToMono(SomeResponse.class);

}

但是我得到了错误:

org.springframework.core.codec.CodecException: No suitable writer found for part: file

我尝试了 MultipartBodyBuilder 的所有变体(部分,asyncpart,带有或不带有标题),但无法正常工作。

我使用错了吗,我想念什么?

关于, 亚历克斯

1 个答案:

答案 0 :(得分:1)

我从Spring框架Github问题部分的一位参与者的回复中找到了解决方案。 为此工作:

  

asyncPart方法需要实际的内容,即file.content()。我将对其进行更新以自动解开零件内容。

bodyBuilder.asyncPart("file", file.content(), DataBuffer.class)
    .headers(h -> {
        h.setContentDispositionFormData("file", file.name());
        h.setContentType(file.headers().getContentType());
    });

如果未设置两个标头,则请求将在远程端失败,表示找不到表单部分。

祝任何需要的人好运!