【问题标题】:HttpMediaTypeNotAcceptableException: Could not find acceptable representation spring mvcHttpMediaTypeNotAcceptableException:找不到可接受的表示spring mvc
【发布时间】:2015-03-03 14:46:54
【问题描述】:

这快把我逼疯了!我正在尝试提供 JPG 图片。我确信这种方法前几天工作得很好,所以我不知道发生了什么变化。我已经尝试了很多不同的方法来让它工作,但我似乎无法摆脱异常。

基本上我正在尝试从数据库中提供图像。

我认为实际字节可能已损坏,因此我将它们写入文件,并检查了文件内容。就在 Mac 上的 Finder 中,临时目录中的文件在预览应用程序中看起来很好,所以我很确定不是内容本身导致了问题。

这是控制器方法:

@RequestMapping(value="/binaries/**", method = RequestMethod.GET, produces={MediaType.APPLICATION_JSON_VALUE, MediaType.IMAGE_GIF_VALUE,
            MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE, "application/javascript"})
    public @ResponseBody
    ResponseEntity<byte[]> serveResource(WebRequest webRequest, HttpServletResponse response, String uri) throws IOException {
        String path = (String)request.getAttribute( HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE );

        BinaryFile bf = binaryService.findByUri(path);
        String tmpdir = System.getProperty("java.io.tmpdir");
        File dest = new File(tmpdir + File.separator + bf.getFileName());
        FileUtils.writeByteArrayToFile(dest, bf.getResource());
        logger.debug("file written: " + dest.getAbsolutePath());
//        response.addHeader("Cache-Control", "public, max-age=3600");

        if (webRequest.checkNotModified(bf.getLastModifiedDate().toDate().getTime()))
        {
            return null;
        };
        return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(bf.getResource());
    }

这是个例外:

Request: http://localhost:8080/binaries/products/shortcode_1/test_image2.jpg raised org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

有人有什么想法吗?这是春天 4.1.4.RELEASE

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

    哦,没关系,我知道发生了什么变化。我已经覆盖了 MessageConverters,因为我正在处理一些 Jackson 的东西,所以我需要手动添加回 ByteArrayHttpMessageConverter。

    @Bean
        public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter(){
            ByteArrayHttpMessageConverter bam = new ByteArrayHttpMessageConverter();
            List<org.springframework.http.MediaType> mediaTypes = new LinkedList<org.springframework.http.MediaType>();
            mediaTypes.add(org.springframework.http.MediaType.APPLICATION_JSON);
            mediaTypes.add(org.springframework.http.MediaType.IMAGE_JPEG);
            mediaTypes.add(org.springframework.http.MediaType.IMAGE_PNG);
            mediaTypes.add(org.springframework.http.MediaType.IMAGE_GIF);
            mediaTypes.add(org.springframework.http.MediaType.TEXT_PLAIN);
            bam.setSupportedMediaTypes(mediaTypes);
            return bam;
        }
    
     @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    
            MappingJackson2HttpMessageConverter mapper = new MappingJackson2HttpMessageConverter();
            ObjectMapper om = new ObjectMapper();
            om.registerModule(new JodaModule());
            om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
            mapper.setObjectMapper(om);
            converters.add(mapper);
    
            converters.add(byteArrayHttpMessageConverter());
    
            super.configureMessageConverters(converters);
        }
    

    【讨论】:

    • 在我的例子中是ResourceHttpMessageConverter。我刚刚像这样添加了它converters.add(new ResourceHttpMessageConverter());,控制器开始返回图像。
    猜你喜欢
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2013-08-08
    • 2015-05-08
    • 2015-09-04
    • 1970-01-01
    • 2022-10-20
    • 2014-04-18
    相关资源
    最近更新 更多