后缀为“ .log”的HttpMediaTypeNotAcceptableException

时间:2019-08-19 15:37:58

标签: spring spring-boot spring-mvc integration-testing

SpringBoot 2.1.7。发布。

发出GET请求以获取扩展名为“ .log”的文件时,引发

“ HttpMediaTypeNotAcceptableException:找不到可接受的表示形式”

如果扩展名在以下代码中更改为“ .zip”或“ .lg”或除“ .log”以外的任何其他字符,它将起作用:

    @Test
    public void testGetLog() throws Exception {
        MockitoAnnotations.initMocks(this);

        mockMvc = MockMvcBuilders
                .standaloneSetup(controller)
                .alwaysDo(print())
                .build();

        Resource resource = mock(Resource.class);
        when(resource.getInputStream()).thenReturn(IOUtils.toInputStream("some test data", "UTF-8"));
        ResponseEntity<Resource> responseEntity = new ResponseEntity<>(resource, HttpStatus.OK);
        when(controllerConnector.getLog(eq(1L), anyString())).thenReturn(responseEntity);

        mockMvc.perform(MockMvcRequestBuilders.get("/log/{id}/{logName}", 1L, "log1.log").contentType(MediaType.ALL)
                .accept(MediaType.ALL))
                .andExpect(status().isOk())
                .andExpect(content().string("some test data"));
    }

和控制器:

    @GetMapping(value = "/{id}/{logName:.+}", produces = "application/zip")
    public ResponseEntity getLog(@PathVariable Long id, @PathVariable String logName) {
        return service.getLog(id, logName);
    }

我尝试配置MVC内容协商,但以下操作并没有改变:

spring.mvc.contentnegotiation.media-types.log=application/octet-stream
spring.mvc.contentnegotiation.favor-path-extension=false
spring.mvc.contentnegotiation.favor-parameter=false

它曾经与SpringBoot 1.5.7一起使用

0 个答案:

没有答案