4

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

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

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 ?

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

Controller Code

 @RestController
    public class GreetingController {

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

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;

    }

Error

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"])
ajayramesh
  • 3,576
  • 8
  • 50
  • 75

1 Answers1

4

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()));
    }

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

Community
  • 1
  • 1
ajayramesh
  • 3,576
  • 8
  • 50
  • 75
  • When trying to access zip files from browser then it is downloaded as "f.txt" instead of the actual zip file name and ".zip" extension. Any idea how to solve that issue? https://github.com/spring-projects/spring-boot/issues/4220 – firstpostcommenter Jul 25 '18 at 16:36