Is produces of @RequestMapping sensitive to order of values?

我是研究僧i 提交于 2020-01-03 02:04:45

问题


This question is based on this question.

With provided comments, i had written three different tests to validate properly set content-types.

@Test
public void testGetImageJpg_ShouldSucceed() throws Exception {
    File testImage = new File(TestConstants.TEST_IMAGE_JPG);
    byte[] expectedBytes = IOUtils.toByteArray(new FileInputStream(testImage));
    when(service.getImage(anyString(), anyString())).thenReturn(testImage);
    mockMvc.perform(get("/getImage/id/bla.jpg").sessionAttrs(session))
            .andExpect(status().isOk()).andExpect(content().contentType(MediaType.IMAGE_JPEG))
            .andExpect(content().bytes(expectedBytes));

}

@Test
public void testGetImagePng_ShouldSucceed() throws Exception {
    File testImage = new File(TestConstants.TEST_IMAGE_PNG);
    byte[] expectedBytes = IOUtils.toByteArray(new FileInputStream(testImage));
    when(service.getImage(anyString(), anyString())).thenReturn(testImage);
    mockMvc.perform(get("/getImage/id/bla.png").sessionAttrs(session))
            .andExpect(status().isOk()).andExpect(content().contentType(MediaType.IMAGE_PNG))
            .andExpect(content().bytes(expectedBytes));

}

@Test
public void testGetImageGif_ShouldSucceed() throws Exception {
    File testImage = new File(TestConstants.TEST_IMAGE_GIF);
    byte[] expectedBytes = IOUtils.toByteArray(new FileInputStream(testImage));
    when(service.getImage(anyString(), anyString())).thenReturn(testImage);
    mockMvc.perform(get("/getImage/id/bla.gif").sessionAttrs(session))
            .andExpect(status().isOk()).andExpect(content().contentType(MediaType.IMAGE_GIF))
            .andExpect(content().bytes(expectedBytes));

}

This is my controller, where all tests succeed:

@RequestMapping(value="/getImage/{id}/{path}", produces = {"image/png","image/jpeg","image/gif"})
@ResponseBody
byte[] getImage(@PathVariable("id") String id,
        @PathVariable("path") String path) throws ImageNotFoundException {      
    File imageFile = service.getImage(id, path);
    InputStream in;        
    try {
        in = new FileInputStream(imageFile);
        return IOUtils.toByteArray(in);
    } catch (IOException e) {           
        throw new ImageNotFoundException();
    }
}

But when I change the order of produces value to

produces = {"image/jpeg","image/png","image/gif"}

The test for png is failing:

java.lang.AssertionError: Content type expected:<image/png> but was:<image/jpeg>

Im little confused, that changing the order of produces values leads to different results.

Does anyone observed this, is it a bug or did I miss something ?

来源:https://stackoverflow.com/questions/39391014/is-produces-of-requestmapping-sensitive-to-order-of-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!