title: Java 8 REST API - Return List of Images from Server
body: <p>I am trying to return a list of images (List&lt;byte[]&gt;) from an API endpoint and get the following error:</p>
  <pre><code>Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
  </code></pre>
  <p>To return a single image from the API, I have the following method that works fine:</p>
  <pre><code>@GetMapping(value = &quot;/getImage/{imageId}&quot;, produces = MediaType.IMAGE_JPEG_VALUE)
  public byte[] getImage(@PathVariable(&quot;imageId&quot;) String imageId) throws IOException {
      final Optional&lt;ImageModel&gt; retrievedImage = imageRepository.findById(Long.valueOf(imageId));
  
      ImageModel img = new ImageModel(retrievedImage.get().getName(), retrievedImage.get().getType(),
              decompressBytes(retrievedImage.get().getPicByte()));
      
      return img.getPicByte();
  }
  </code></pre>
  <p>However, I am now looking for something similar but rather than return 1 image, I want to return all images related to the productId. The Database side is all setup correctly, but I get the error previously mentioned when trying to call the following endpoint:</p>
  <pre><code>@GetMapping(value = &quot;/getImage/{productId}&quot;, produces = MediaType.IMAGE_JPEG_VALUE)
  public List&lt;byte[]&gt; getImagesForProduct(@PathVariable(&quot;productId&quot;) int productId) throws IOException {
      final Optional&lt;List&lt;ImageModel&gt;&gt; retrievedImages = imageRepository.getByProductId(String.valueOf(productId));
      
      List&lt;ImageModel&gt; imageModelList = retrievedImages.get();
      List&lt;byte[]&gt; response = new ArrayList&lt;byte[]&gt;();
      
      imageModelList.forEach(image -&gt; {
          response.add(image.getPicByte());
      });
      
      return response;
  }
  </code></pre>
  <p>From my understanding when using <code>MediaType.IMAGE_JPEG_VALUE</code> this tells the browser how to display the bytes correctly (Which it does) but is there anything similar for returning a list of images?</p>
  <p>I have tried creating a wrapper class so that I can return the imageBytes &amp; imageName together as follows:</p>
  <pre><code>public class ProductImageResponse {
  
  private String productName;
  
  private byte[] imageBytes;
  
  public String getProductName() {
      return productName;
  }
  
  public void setProductName(String productName) {
      this.productName = productName;
  }
  
  public byte[] getImageBytes() {
      return imageBytes;
  }
  
  public void setImageBytes(byte[] imageBytes) {
      this.imageBytes = imageBytes;
  }
  }
  
  
      @GetMapping(value = &quot;/getImage/{imageId}&quot;, produces = MediaType.IMAGE_JPEG_VALUE)
  public List&lt;ProductImageResponse&gt; getImagesForProduct(@PathVariable(&quot;productId&quot;) String productId) throws IOException {
      final Optional&lt;List&lt;ImageModel&gt;&gt; retrievedImages = imageRepository.getByProductId(productId);
      
      List&lt;ImageModel&gt; imageModelList = retrievedImages.get();
      List&lt;ProductImageResponse&gt; response = new ArrayList&lt;ProductImageResponse&gt;();
      
      imageModelList.forEach(image -&gt; {
          ProductImageResponse productImageResponse = new ProductImageResponse();
          productImageResponse.setProductName(image.getName());
          productImageResponse.setImageBytes(image.getPicByte());
      });
      
      return response;
  }
  </code></pre>
  <p>But i get the same error.</p>