HttpMediaTypeNotAcceptableException:在异常处理程序中找不到可接受的表示形式

2022-09-02 09:11:44

我的控制器中有以下图像下载方法(Spring 4.1):

@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);
}

以下方法应处理不存在的文件并返回 json 错误响应:ControllerAdvice

@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public @ResponseBody Map<String, String> handleResourceNotFoundException(ResourceNotFoundException e) {
    Map<String, String> errorMap = new HashMap<String, String>();
    errorMap.put("error", e.getMessage());
    return errorMap;
}

我的 JUnit 测试完美无瑕

(编辑这是因为扩展 .bla :这也适用于应用程序服务器):

@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));
}

并给出以下输出:

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 = []

但是,在我的应用程序服务器上,尝试卸载不存在的图像时,我会收到以下错误消息:

(编辑 这是因为扩展.jpg:这在具有.jpg扩展的JUnit测试中也失败):

ERROR org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Failed to invoke @ExceptionHandler method: public java.util.Map<java.lang.String, java.lang.String> nl.krocket.ocr.web.controller.ExceptionController.handleResourceNotFoundException(nl.krocket.ocr.web.backing.ResourceNotFoundException) org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

我已经在我的mvc配置中配置了消息转换器,如下所示:

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> 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;
}

我错过了什么?为什么 JUnit 测试有效?


答案 1

您需要决定Spring应如何确定响应的媒体类型。这可以通过几种方式完成:

  • 路径扩展(例如 /image.jpg)
  • URL 参数(例如 ?format=jpg)
  • HTTP接受标头(例如。接受:图片/jpg)

默认情况下,Spring 查看扩展而不是标头。如果您实现扩展WebMvcConfigurerAdapter的类(或者从Spring 5.0开始简单地实现WebMvcConfigurer),则可以更改此行为。在那里,您可以覆盖 configureContentNegotiation(ContentNegotiationConfigurer configurer)并根据您的需要配置ContentNegotiationConfigurer,例如。致电Accept@Configuration

ContentNegotiationConfigurer#favorParameter
ContentNegotiationConfigurer#favorPathExtension

如果将两者都设置为 ,则 Spring 将查看标头。由于你的客户端可以说并处理两者,所以Spring应该能够返回图像或错误的JSON。falseAcceptAccept: image/*,application/json

请参阅有关内容协商的春季教程,以获取更多信息和示例。


答案 2

注意您的 HTTP 接受标头。例如,如果您的控制器生成“application/octet-stream”(作为响应),则 Accept 标头不应为“application/json”(在请求中):

@GetMapping(value = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void download(HttpServletResponse response) {}

推荐