java-如何在Spring Rest Call中使用GridFS从Mongo发送检索到的图像?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何在Spring Rest Call中使用GridFS从Mongo发送检索到的图像?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2627字,纯文字阅读大概需要4分钟。
内容图文
![java-如何在Spring Rest Call中使用GridFS从Mongo发送检索到的图像?](/upload/InfoBanner/zyjiaocheng/663/0d10d7ca2e4941c88b32241303ff95a5.jpg)
我已经使用Spring Data和GridFs Template从Mongo DB中检索了图像
所以我不知道如何将检索到的输入流提供给用户.
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中共享用于处理图像请求的任何代码存储库?
控制器代码
@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 4.1的最新版本(例如Spring 4.1),则我已经使用过Spring Boot和Rest了,下面的代码可以正常工作
@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: How to return image in @ResponseBody?
内容总结
以上是互联网集市为您收集整理的java-如何在Spring Rest Call中使用GridFS从Mongo发送检索到的图像?全部内容,希望文章能够帮你解决java-如何在Spring Rest Call中使用GridFS从Mongo发送检索到的图像?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。