REST API Return Image With Spring

By | July 31, 2018

How to create rest controller for return image file?

Here is the code for create image rest API with spring:

@GetMapping(
      value = "/img", 
      produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_GIF_VALUE, MediaType.IMAGE_PNG_VALUE})
  public ResponseEntity getImageAsResource() {
    HttpHeaders headers = new HttpHeaders();
    String home = System.getProperty("user.home");
    File f = new File(home + File.separator + "Desktop" + File.separator + "image.jpg");
    Resource resource = new FileSystemResource(f);
    return new ResponseEntity<>(resource, headers, HttpStatus.OK);
  }

REST API Return Image With Spring

REST API Return Image With Spring

Just comment bellow if you don’t know to implement this code.

Leave a Reply

Your email address will not be published. Required fields are marked *