Returning TIFF Image Data with Spring MVC

The Tagged Image File Format (TIFF or TIF) is a computer file format for storing raster graphics images. If you want to deliver TIFF data with Spring MVC, you basically return the bytes you receive from an input stream to the client.

When the client sends a request with an image/tiff accept header, you send a response back with the content length, the expected content type and the bytes of the TIFF. You can use the following handler method as a reference.

@GetMapping(value="/your-tiff-mapping", headers="accept=image/tiff",produces="image/tiff")
public ResponseEntity getTiffImage() {
// [...] call some use case and get the output data [...]
  return ResponseEntity

For caching purposes you may also want to set the Cache-ControlPragmaExpires headers.

Furthermore, you need to provide a ByteArrayHttpMessageConverter that can handle byte array response data. Create a configuration that implements the WebMvcConfigurer interface and overrides the configureMessageConvertersmethod to register your converter for the image/tiff media type.

public class WebMvcMessageConvertersConfiguration implements WebMvcConfigurer {

  public void configureMessageConverters(
    List<HttpMessageConverter<?>> messageConverters
  ) {
    ByteArrayHttpMessageConverter byteArrayConverter =
      new ByteArrayHttpMessageConverter();

s That's it. You are done. It is basically the same for other image formats. Have a look at another article for an alternative implementation.