How to return file (byte array) from spring controllers

Valery Yakovlev
Sep 6, 2018 · 1 min read

Here comes an example how one can retrieve file (byte array) from the spring controller using ResponseEntity of byte[]

@GetMapping(value = "/{id}")
public ResponseEntity<byte[]> get(@PathVariable UUID id) {

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
return new ResponseEntity<>(service.get(id), headers, HttpStatus.OK);
}

Valery Yakovlev

Written by

Notes on Java development. Tech: Java 8, Spring Boot, Postgres, Cassandra, Elasticsearch

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade