如何在 Spring Rest Call 中使用 GridFS 发送从 Mongo 检索到的图像?
How to send an retrieved image from Mongo using GridFS in Spring Rest Call?
我使用 Spring 数据和 GridFs 模板
从 Mongo 数据库中检索了图像
所以我不知道如何将检索到的输入流返回给用户。
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 中的图像请求的代码存储库?
Controller Code
@RestController
public class GreetingController {
@RequestMapping("/image")
public GridFSDBFile imageReponse() {
App.getImage();
return App.getImageResponse();
}
}
此函数将从 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 引导和休息,如果您使用的是最新版本的 spring,即 Spring 4.1[=,则以下代码将起作用17=]
@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()));
}
我关注了这个post,看看。
Spring MVC: How to return image in @ResponseBody?
我使用 Spring 数据和 GridFs 模板
从 Mongo 数据库中检索了图像所以我不知道如何将检索到的输入流返回给用户。
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 ?
您能否分享任何用于处理 Rest Call 中的图像请求的代码存储库?
Controller Code
@RestController
public class GreetingController {
@RequestMapping("/image")
public GridFSDBFile imageReponse() {
App.getImage();
return App.getImageResponse();
}
}
此函数将从 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 引导和休息,如果您使用的是最新版本的 spring,即 Spring 4.1[=,则以下代码将起作用17=]
@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()));
}
我关注了这个post,看看。 Spring MVC: How to return image in @ResponseBody?