Spring HttpMediaTypeNotAcceptableException

Spring HttpMediaTypeNotAcceptableException,spring,spring-boot,Spring,Spring Boot,我正在尝试为下载视频文件创建一个端点。代码如下: @GetMapping(value = "/video") public ResponseEntity getVideo() throws MalformedURLException { FileSystemResource fileSystemResource = new FileSystemResource("E:\\video\\hello.mp4"); ResourceRegion region =

我正在尝试为下载视频文件创建一个端点。代码如下:

@GetMapping(value = "/video")
    public ResponseEntity getVideo() throws MalformedURLException {
        FileSystemResource fileSystemResource = new FileSystemResource("E:\\video\\hello.mp4");
        ResourceRegion region = new ResourceRegion(fileSystemResource,
                                                   0,
                                                   fileSystemResource.getFile().length());
        return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
                .contentType(MediaType.valueOf("video/mp4"))
                .contentLength(fileSystemResource.getFile().length())
                .body(region);
    }
我在尝试调用url时收到以下响应:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

事先感谢您的帮助

1无需资源区域

    @GetMapping(value = "/video")
    public ResponseEntity getVideo() throws MalformedURLException {
        FileSystemResource fileSystemResource = new FileSystemResource("E:\\video\\hello.mp4");
//        ResourceRegion region = new ResourceRegion(fileSystemResource, 0, fileSystemResource.getFile().length());
        return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
                .contentType(MediaType.valueOf("video/mp4"))
                .contentLength(fileSystemResource.getFile().length())
                .body(fileSystemResource);
    }
2或将转换器添加到弹簧:

@Configuration
public class ApplicationConfig extends WebMvcConfigurationSupport {

    @Bean
    public ResourceRegionHttpMessageConverter regionMessageConverter() {
       return new ResourceRegionHttpMessageConverter();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(regionMessageConverter());
        super.configureMessageConverters(converters);
    }
}
使用fix控制器:

@GetMapping(value = "/video")
public ResponseEntity<ResourceRegion> getVideo() {
    FileSystemResource fileSystemResource = new FileSystemResource("E:\\video\\hello.mp4");
    ResourceRegion region = new ResourceRegion(fileSystemResource, 0, fileSystemResource.getFile().length());
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT)
            .contentType(MediaType.valueOf("video/mp4"))
            .contentLength(fileSystemResource.getFile().length())
            .body(region);
}

您是否尝试在@Getmapping注释中指定products=MediaType.valueOfvideo/mp4?您的第一个解决方案成功了,我只需将状态代码从部分内容更改为ok即可。再次感谢。