Фон
Я пытаюсь получить доступ к изображениям, содержащимся в classpath:resources/images
. В каталоге изображений содержатся подкаталоги.
Issue
При доступе, например, localhost:<springboot-port>/images/some-sub-directory/some-image.png
Мне выдается ошибка:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Apr 06 19:52:20 UTC 2020
There was an unexpected error (type=Not Found, status=404).
SRVE0295E: Error reported: 404
Как я могу получить доступ к изображениям из подкаталогов таким способом?
Код
@RestController
public class ImagesController {
@CrossOrigin
@RequestMapping(value = "/images/{filePath}", method = RequestMethod.GET, produces = MediaType.IMAGE_PNG_VALUE)
public void getImage(@PathVariable("filePath") String filePath, HttpServletResponse response) throws IOException {
response.setContentType(MediaType.IMAGE_PNG_VALUE);
StreamUtils.copy(new ClassPathResource("images/" + filePath).getInputStream(), response.getOutputStream());
}
}