如何在Spring Rest Call中使用GridFS从Mongo发送检索到的图像? [英] How to send an retrieved image from Mongo using GridFS in Spring Rest Call?

查看:181
本文介绍了如何在Spring Rest Call中使用GridFS从Mongo发送检索到的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring Data和GridFs Template

I have retrieved the image from Mongo DB using Spring Data and GridFs Template

从Mongo DB中检索图像,所以我不知道如何将检索到的输入流提供给用户。

so i don't know how to serve that retrieved input stream back to user .



假设他们要求 http://host.com/apple 作为春季休息电话
现在我的应用程序使用名称 apple 处理请求,它从mongodb数据库中检索苹果图像。
现在不保存任何地方我想将响应显示为用户的图像,显​​示 http://host.com/apple 浏览器中的图像。
我究竟需要实现这个?

Say they requested for the http://host.com/apple as a spring rest call . Now my application process the request by using the name apple it retrieves the apple image from a mongodb database . Now without saving anywhere i want to display the response as an image to user that will show http://host.com/apple image in the browser. How exactly i need to implement this ?


您能否共享任何代码存储库以便在Rest Call中处理图像请求?

Could you please share any code repository for processing the image request in Rest Call ?

控制器代码

 @RestController
    public class GreetingController {

    @RequestMapping("/image")
    public GridFSDBFile imageReponse() {
        App.getImage();
        return App.getImageResponse();
    }
}

此函数将从mongodb中获取图像

This function will fetch the image from the mongodb

public static GridFSDBFile getImageResponse() {
        try {

            ApplicationContext context = new FileSystemXmlApplicationContext(
                    "file:C:\\workspace\\gs-rest-service-complete\\spring-config.xml");
            FileStorageDao fileStorageDao = (FileStorageDao) context
                    .getBean("fileStorageDao");

            GridFSDBFile retrive = fileStorageDao.retrive("audi.jpg");
            return retrive;
        } catch (Exception e) {
            System.out.println("IOException:-" + e.getMessage());
        } finally {
            System.out.println("Clean up herer:-");
        }
        return null;

    }

错误

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Sep 04 17:21:05 IST 2015
There was an unexpected error (type=Internal Server Error, status=500).
Could not write content: No serializer found for class com.mongodb.gridfs.GridFSDBFile$MyInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.mongodb.gridfs.GridFSDBFile["inputStream"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.mongodb.gridfs.GridFSDBFile$MyInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.mongodb.gridfs.GridFSDBFile["inputStream"])


推荐答案

如果您使用的是最新版本的弹簧,我使用了弹簧靴和休息,以下代码将起作用,即 Spring 4.1

I have used the spring boot and rest where this following code will work if you are using latest version of spring i.e.,Spring 4.1

@RequestMapping(value = "/image", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<InputStreamResource> getImage() {
        GridFSDBFile gridFsFile = App.getImageResponse();

        return ResponseEntity.ok()
                .contentLength(gridFsFile.getLength())
                .contentType(MediaType.parseMediaType(gridFsFile.getContentType()))
                .body(new InputStreamResource(gridFsFile.getInputStream()));
    }

我关注此帖子,退房。
Spring MVC:如何在@ResponseBody中返回图片?

I followed this post , Check out . Spring MVC: How to return image in @ResponseBody?

这篇关于如何在Spring Rest Call中使用GridFS从Mongo发送检索到的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆