简体   繁体   中英

Serving static images in Spring Boot

I'm trying very simple scenario:

I generated a Spring Boot (v1.3.3) project using start.spring.io initialiser having selected Web dependency. I have then put sample JS, CSS and PNG files into resources/static folder and started up the webapp.

Now when I try to access JS and CSS resources everything's fine, but for the image it throws org.springframework.web.HttpMediaTypeNotAcceptableException (Could not find acceptable representation) and returns error 406.

Is there any configuration to be done to serve static images from resources/static folder?

No further configuration should be required. (I'm also serving images from the static folder of a Spring Boot v1.3.3 app.)

How are you accessing the images? Are you calling servletContext.getResourceAsStream("filename") from a Controller and returning a byte array? If that's the case you may need to add eg produces = MediaType.IMAGE_JPEG_VALUE to your @RequestMapping annotation.

If using JSPs you should just be able to use the Spring url tag in the following manner:

<spring:url value="/path/to/your-image.jpg" var="logoImg"/>
<img src="${logoImg}">

..where the path starts from within the static folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM