![](/img/trans.png)
[英]Spring WebClient multipart/form-data request, Could not able to send file
[英]Spring WebClient. Receive multipart/form-data
現有的用於多部分/表單數據的BodyExtractors
可與 WebClient 一起使用。
我發現:
BodyExtractors.toMultipartData()
)但是我找不到通過 WebClient 解析多部分響應正文的解決方案。
您必須直接使用WebClient
將其注冊為自定義編解碼器。
例如,在您的 WebClient bean 中:
return WebClient.builder()
.codecs(clientCodecConfigurer -> clientCodecConfigurer.defaultCodecs().maxInMemorySize(1 * 1024 * 1024)) // How to change defaults
.codecs(clientCodecConfigurer -> clientCodecConfigurer.customCodecs()
.register(new MultipartHttpMessageReader(new DefaultPartHttpMessageReader())) // <-- Add this custom codec
)
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE) // How to set default header
.baseUrl("https://foo.local") // How to set Base URL in builder
.build();
這會為此WebClient
注冊MultipartHttpMessageReader
。
如果您只預制了一個 WebClient 實例,您仍然可以對其進行變異:
MultiValueMap<String, Part> response = webClient
.mutate() // This creates a pre-initialized builder
.codecs(clientCodecConfigurer -> clientCodecConfigurer.customCodecs()
.register(new MultipartHttpMessageReader(new DefaultPartHttpMessageReader())) // <-- Add our codec
)
.build() // Build a new instance
.get() // Whatever is your HTTP method
.accept(MediaType.MULTIPART_FORM_DATA)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<MultiValueMap<String, Part>>() {
/* Freeze generic types. You need the exact type, as the Codec looks into the key and value types as well. */
})
.block();
這樣,您可以保留來自先前構建器的值(例如,OAuth2 過濾器)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.