本文介紹了字節跳動睡覺模板的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用Spring框架REST模板獲取字節數組,
但我還需要獲取此字節的MediaType。
此字節數組的MediaType可以是任何類型。
現在用于獲取字節的代碼如下。
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.valueOf("application/pdf")));
ResponseEntity<byte[]> result = restTemp.exchange(url, HttpMethod.GET, entity, byte[].class,documentId);
以上代碼將僅獲取pdf內容類型。
如何將Content Type設置為接受任何泛型MediaType,因為另一端的服務正在為byteArray提供任何隨機的MediaType。
請建議如何獲取媒體類型。
歡迎提出任何建議..
推薦答案
只是不發送accept
頭以不強制服務器返回該content-type
。這與發送通配符*/*
相同
//HttpHeaders headers = new HttpHeaders();
//headers.setAccept(Collections.singletonList(MediaType.WILDCARD));
ResponseEntity<byte[]> result = restTemp.exchange(url, HttpMethod.GET, entity, byte[].class,documentId);
之后,從響應的標頭中提取content-type
byte body[] = result.getBody();
MediaType contentType = result.getHeaders().getContentType();
這篇關于字節跳動睡覺模板的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,