1

I have this endpoint:

@GetMapping("/thumbnail/{imageName}")
@PreAuthorize("hasRole('BASIC')")
public ResponseEntity<InputStreamResource> natalChartThumbnail() throws IOException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    // 🔹 Generate thumbnail
    BufferedImage bufferedImage = Thumbnails.of(new File("/usr/local/bin/tmp/scaled_sun_in_cancer_moon_in_aquarius.jpg"))
            .scale(1.0)
            .asBufferedImage();

    ImageIO.write(bufferedImage, "jpg", os);
    InputStream is = new ByteArrayInputStream(os.toByteArray());

    return ResponseEntity.ok()
            .contentType(MediaType.parseMediaType("image/jpg"))
            .body(new InputStreamResource(is));

}

But in Postman, I see this:

enter image description here

and imageExtension: jpg

I also tried with:

 return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType("image/jpeg"))
                .body(new InputStreamResource(is));

with the same result

5
  • 2
    Did you check the received Content-Type? Commented Mar 8, 2025 at 11:56
  • 2
    Your edit with image/jpeg is equivalent to the answer that suggests setting the produces attribute of the @GetMapping annotation. Are you sure it didn't work? As ^^ Olivier asks, what did you receive as a content-type response header? Commented Mar 14, 2025 at 13:42
  • 1
    Also, you don't need to parse that string. There's a MediaType constant for it already, in MediaType.IMAGE_JPEG. Commented Mar 14, 2025 at 13:45
  • 1
    Agree with @SotiriosDelimanolis. Setting the Content-Type using produces or contentType leads to the same result. The issue persisted even after applying Olivier’s suggestion due to Postman caching. I've managed to reproduce it. And I guess you can as well Commented Mar 14, 2025 at 13:53
  • This question is linked on meta Commented Mar 25, 2025 at 13:21

3 Answers 3

4
How to resolve the issue

The following 2 steps have to resolve your issue:

  1. Set the correct Content-Type by contentType(MediaType.IMAGE_JPEG)
  2. Make Postman to forget incorrect Content-Type.
    Perform any of the following. No need to do all of them:
    • run Clear Response enter image description here
    • run Menu Bar -> Help -> Clear Cache and Reload enter image description here
    • make a request in a new tab in Postman
    • completely close and open Postman
Why is setting the correct header not enough?

The issue persists after just changing Content-Type due to Postman caching.
There were similar GitHub issues:

How to reproduce the problem and fix:
  1. Set incorrect Content-Type (image/jpg), the preview fails
  2. Set correct Content-Type (image/jpeg), the preview still fails
  3. Open a new tab in Postman and make the same request, the preview successfully displays the requested picture
Additional notes:

The SO question regarding the correct header - Is the MIME type 'image/jpg' the same as 'image/jpeg'?.

To set the header Content-Type: image/jpeg, you can use any of the following options, as they all lead to the same result:

  • ResponseEntity.ok().contentType(MediaType.parseMediaType("image/jpeg"))
  • ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG)
  • Setting produces = MediaType.IMAGE_JPEG_VALUE in an annotation for mapping HTTP requests

You can test this by looking to returned headers using:

curl -i http://localhost:8080/thumbnail/imageName
Sign up to request clarification or add additional context in comments.

Comments

2
+300

I found the issue. You didn't try this option given below:

produces = MediaType.IMAGE_JPEG_VALUE

Please try the above option. I have cleaned up your code a little bit. So, modify your code based on the code given below:

package com.example;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.springframework.core.io.InputStreamResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import net.coobird.thumbnailator.Thumbnails;

@RestController
public class SampleController {
    
    @GetMapping(value = "/thumbnail/{imageName}", produces = MediaType.IMAGE_JPEG_VALUE)
    public ResponseEntity<InputStreamResource> natalChartThumbnail(@PathVariable String imageName) throws IOException {

        ByteArrayOutputStream os = new ByteArrayOutputStream();

        // 🔹 Generate thumbnail
        BufferedImage bufferedImage = Thumbnails.of(new File("/Users/anisb/" + imageName + ".jpg"))
                .scale(1.0)
                .asBufferedImage();

        ImageIO.write(bufferedImage, "jpg", os);
        InputStream in = new ByteArrayInputStream(os.toByteArray());

        return ResponseEntity.ok().body(new InputStreamResource(in));

    }
}

This works.

Postman Screenshot:

enter image description here

Comments

2

Did you try to change image/jpg to image/jpeg. image/jpg is not a valid Content-Type.

Comments

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.