406 on returned image

时间:2015-07-28 23:04:56

标签: spring-mvc spring-boot

I have a Spring Boot app that acts as an image server. I am getting a 406 when I try to return the mongodb stored image.

here is the error that I get:

Method [error] returned [<406 Not Acceptable,{timestamp=Tue Jul 28 16:50:57 MDT 2015, status=406, error=Not Acceptable, exception=org.springframework.web.HttpMediaTypeNotAcceptableException, message=Could not find acceptable representation, path=/images/1438123819971_10420419_10204541009433090_4380886302080092329_n.jpg},{}>]

and here is the GET method:

@RequestMapping(value = "images/{filename}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity getSizedImage(@PathVariable String filename, @RequestParam int width, @RequestParam int height) throws Exception {

        GridFSDBFile savedFile = mongoFileService.getStore(filename);
        if (savedFile != null) {
            BufferedImage image = ImageIO.read(savedFile.getInputStream());
            image = resize(image, Method.SPEED, width, height, Scalr.OP_ANTIALIAS);

            LOGGER.info("Returning Filename " + savedFile.getFilename() + " sized to " + width + " X " + height);
            return ResponseEntity.ok().contentLength(savedFile.getLength())
                            .contentType(MediaType.parseMediaType(MediaType.IMAGE_JPEG_VALUE)).body(image);
        } else {
            LOGGER.error("Error retrieving file " + filename );
            return ResponseEntity.status( HttpStatus.INTERNAL_SERVER_ERROR )
                            .contentType( MediaType.TEXT_PLAIN).body(MISSING_IMAGE_PATH );
        }
    }

Any ideas why this image won't return in my ResponseBody?

Also, I noticed that the file extension doesn't appear as part of the filename parameter. I can manipulate it in the debugger, appending a ".jpg" and the file is retrieved and resized.

UPDATE Here was the solution that worked for me, based on feedback:

@RequestMapping(value = "images/{filename}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity getPatientSizedImage(@RequestParam String filename, @PathVariable int width, @PathVariable int height) throws Exception {

        GridFSDBFile savedFile = mongoFileService.getStore( filename );
        BufferedImage image = ImageIO.read( savedFile.getInputStream() );
        image = resize( image, Method.SPEED, width, height, Scalr.OP_ANTIALIAS );

        LOGGER.info( "Returning Filename " + savedFile.getFilename() + " sized to " + width + " X " + height );
        return ResponseEntity.ok().contentLength( savedFile.getLength() )
                .contentType( MediaType.parseMediaType( MediaType.IMAGE_JPEG_VALUE ) ).body( image );
    }

1 个答案:

答案 0 :(得分:3)

根据你的问题:

  

例外= org.springframework.web.HttpMediaTypeNotAcceptableException,   message =无法找到可接受的表示,path = / images

您应该在控制器中添加produce = MediaType.IMAGE_JPEG_VALUE

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

首先注册一个ByteArrayHttpMessageConverter,

这是适合我的代码,

@RequestMapping("/image", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> image() throws IOException {
    InputStream in = servletContext.getResourceAsStream("/images/no_image.jpg");

    final HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.IMAGE_PNG);

    return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
}