简体   繁体   English

用于字节的Spring REST模板

[英]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 . 我使用spring框架rest模板获取字节数组,但我还需要获取此字节的Mediatype。

The mediaType of this bytearray can be of any type. 此bytearray的mediaType可以是任何类型。

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. 以上代码仅获取pdf内容类型。

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. 如何将contentType设置为接受任何通用MediaType,因为另一端的服务为byteArray提供任何随机MediaType。

Could someone please suggest how the MediaType can be fetched. 有人可以建议如何获取MediaType。

Any suggestions are welcome.. 欢迎任何建议..

Just not send the accept header to not force the server to return that content-type . 只是不发送accept标头不强制服务器返回该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 在此之后,从响应的标题中提取content-type

 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. 您可以在HTTP标头中存储媒体类型,并使用 - ResponseEntity.getHeaders()。 or you can wrap byte array and media type into holder class. 或者您可以将字节数组和媒体类型包装到holder类中。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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