简体   繁体   中英

Spring REST Template for Byte

I am fetching the byte array using spring framework rest template, But I also need to fetch the Mediatype of this byte .

The mediaType of this bytearray can be of any type.

The code used now for fetching byte is below.

   HttpHeaders headers = new HttpHeaders();
   headers.setAccept(Collections.singletonList(MediaType.valueOf("application/pdf")));
   ResponseEntity<byte[]> result = restTemp.exchange(url, HttpMethod.GET, entity, byte[].class,documentId);

The above code will fetch only pdf content type.

How to set the contentType to accept any generic MediaType because the service at the other end is providing any random MediaType for the byteArray.

Could someone please suggest how the MediaType can be fetched.

Any suggestions are welcome..

Just not send the accept header to not force the server to return that content-type . This is the same as sending a wildcard */*

//HttpHeaders headers = new HttpHeaders();
//headers.setAccept(Collections.singletonList(MediaType.WILDCARD));
ResponseEntity<byte[]> result = restTemp.exchange(url, HttpMethod.GET, entity, byte[].class,documentId);

After this, extract the content-type from the headers of the response

 byte body[] = result.getBody();
 MediaType contentType = result.getHeaders().getContentType();

You can store a media type in the HTTP headers and use - ResponseEntity.getHeaders(), for instance. or you can wrap byte array and media type into holder class.

MediaType mediaType = result.getHeaders()。getContentType();

您可以将MediaType设置为application/octet-stream ,请在此处查看

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM