put image to spring code example

Example: show image with sprinboot

@GetMapping("database/{id}")
public ResponseEntity<byte[]> fromDatabaseAsResEntity(@PathVariable("id") Integer id) 
        throws SQLException {

	Optional<PrimeMinisterOfIndia> primeMinisterOfIndia = imageRepository.findById(id);
	byte[] imageBytes = null;
	if (primeMinisterOfIndia.isPresent()) {
	
		imageBytes = primeMinisterOfIndia.get().getPhoto().getBytes(1,
					(int) primeMinisterOfIndia.get().getPhoto().length());
	}
	
	return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(imageBytes);
}

Tags:

Misc Example