title: HttpMediaTypeNotAcceptableException: Could not find acceptable representation in exceptionhandler
body: <p>I have the following image download method in my controller (Spring 4.1):</p>
  
  <pre><code>@RequestMapping(value = "/get/image/{id}/{fileName}", method=RequestMethod.GET)
  public @ResponseBody byte[] showImageOnId(@PathVariable("id") String id, @PathVariable("fileName") String fileName) {
      setContentType(fileName); //sets contenttype based on extention of file
      return getImage(id, fileName);
  }
  </code></pre>
  
  <p>The following <code>ControllerAdvice</code> method should handle a non-existing file and return a json error response:</p>
  
  <pre><code>@ExceptionHandler(ResourceNotFoundException.class)
  @ResponseStatus(HttpStatus.NOT_FOUND)
  public @ResponseBody Map&lt;String, String&gt; handleResourceNotFoundException(ResourceNotFoundException e) {
      Map&lt;String, String&gt; errorMap = new HashMap&lt;String, String&gt;();
      errorMap.put("error", e.getMessage());
      return errorMap;
  }
  </code></pre>
  
  <p>My JUnit test works flawless </p>
  
  <p>(<strong>EDIT</strong> this is because of extention .bla : this also works on appserver):</p>
  
  <pre><code>@Test
  public void testResourceNotFound() throws Exception {
      String fileName = "bla.bla";
        mvc.perform(MockMvcRequestBuilders.get("/get/image/bla/" + fileName)
              .with(httpBasic("test", "test")))
              .andDo(print())
              .andExpect(jsonPath("$error").value(Matchers.startsWith("Resource not found")))
              .andExpect(status().is(404));
  }
  </code></pre>
  
  <p>and gives the following output:</p>
  
  <pre><code>MockHttpServletResponse:
            Status = 404
     Error message = null
           Headers = {X-Content-Type-Options=[nosniff], X-XSS-Protection=[1; mode=block], Cache-Control=[no-cache, no-store, max-age=0, must-revalidate], Pragma=[no-cache], Expires=[0], X-Frame-Options=[DENY], Content-Type=[application/json]}
      Content type = application/json
              Body = {"error":"Resource not found: bla/bla.bla"}
     Forwarded URL = null
    Redirected URL = null
           Cookies = []
  </code></pre>
  
  <p>However on my appserver i get the following error message when trying to donwload non existing image: </p>
  
  <p>(<strong>EDIT</strong> this is because of extention .jpg : this also fails on JUnit test with .jpg extention):</p>
  
  <p><code>ERROR org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver  - Failed to invoke @ExceptionHandler method: public java.util.Map&lt;java.lang.String, java.lang.String&gt; nl.krocket.ocr.web.controller.ExceptionController.handleResourceNotFoundException(nl.krocket.ocr.web.backing.ResourceNotFoundException)
  org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation</code></p>
  
  <p>I have configured messageconverters in my mvc configuration as follows:</p>
  
  <pre><code>@Override
  public void configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) {
      converters.add(mappingJackson2HttpMessageConverter());
      converters.add(byteArrayHttpMessageConverter());
  }
  
  @Bean
  public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
      ObjectMapper objectMapper = new ObjectMapper();
      objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
      objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
      //objectMapper.registerModule(new JSR310Module());
      MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
      converter.setObjectMapper(objectMapper);
      converter.setSupportedMediaTypes(getJsonMediaTypes());
      return converter;
  }
  
  @Bean
  public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
      ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
      arrayHttpMessageConverter.setSupportedMediaTypes(getImageMediaTypes());
      return arrayHttpMessageConverter;
  }
  </code></pre>
  
  <p>What am i missing? And why is the JUnit test working?</p>