I am building a web app for blogging using spring boot and mongodb.I tried gridFS for storing and used input stream for storing data. But I can't return image as a model attribute.It's my controller class
@RestController
public class HomeController {
@Autowired
GridFsTemplate gridFsTemplate;
@RequestMapping(value = "/home", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<InputStreamResource> getImage() {
GridFSDBFile gridFsFile = gridFsTemplate.findOne(new Query().addCriteria(Criteria.where("metadata.user").is("5")));;
InputStream inputStream = gridFsFile.getInputStream();
System.out.println("done");
return ResponseEntity.ok()
.contentLength(gridFsFile.getLength())
.contentType(MediaType.parseMediaType(gridFsFile.getContentType()))
.body(new InputStreamResource(gridFsFile.getInputStream()));
}
}
and this is my view
<html xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<img th:src="@{/home}" />
</body>
</html>