2015-09-04 27 views
3

我已經從Mongo DB中使用Spring Data和GridFs檢索圖像如何在Spring Rest Call中使用GridFS從Mongo發送檢索的圖像?

所以我不知道如何將檢索到的輸入流提供給用戶。

說,他們要求爲http://host.com/apple彈簧安置通話。 現在我的應用程序使用名稱apple處理請求,它從mongodb數據庫中檢索蘋果圖像。 現在沒有保存任何地方我想要顯示響應作爲圖像的用戶,將顯示http://host.com/apple圖像在瀏覽器中。 我到底需要如何實施?

請問您是否可以分享任何代碼庫來處理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"]) 

回答

4

我已經使用的彈簧引導和休息的地方這下面的代碼將工作如果您使用的是最新版本的彈簧即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: How to return image in @ResponseBody?

相關問題