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 nameapple
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 showhttp://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"])