![](/img/trans.png)
[英]Spring Boot - WebClient - multipart response - How to get the binary data
[英]How to consume binary and String Response using WebClient in Spring boot?
我需要使用供應商 API,它在請求成功時以二進制格式返回文件(.pdf/.jpg/.png),而在請求失敗時返回 JSON 響應。 Request 方法的類型是 POST。
我嘗試使用以下代碼:
WebClient webClient = WebClient.create();
ResponseEntity<Object> apiResponse = webClient.post()
.uri(new URI("https://api.myapp.in/getDocument"))
.header("mobile", "XXXXXXXXX8")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
//.accept(MediaType.parseMediaType("application/pdf"))
.body(BodyInserters.fromFormData(map))
.retrieve()
.toEntity(Object.class)
.block();
當我執行上面的代碼時,它工作正常,我能夠獲得錯誤情況的 JSON 響應,但是當請求成功時,它會給出如下錯誤:
Content type 'application/pdf' not supported
一旦你得到響應,你就可以ResponseEntity.getHeaders()
。
從響應標頭中,您可以獲得內容類型並相應地使用您的響應。 如果 API 沒有根據響應返回有效的內容類型,您可以在 API 提供者上提出缺陷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.