简体   繁体   中英

Spring boot serving static resource

I work on spring boot application. I'm trying to serve static content with spring. want to serve a resource stored in the /c:/frontend/files/ directory whenever a request comes in for the URL matching the pattern: /file/**:

 @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

    registry
            .addResourceHandler("/file/**")
            .addResourceLocations("file:///C:/frontend/files/" );

}

but when i try to access to this resource using this url: http://localhost:9999/file/app.min.js

I have this problem

There was an unexpected error (type=Not Acceptable, status=406).
Could not find acceptable representation

I resolved the problem. it's related to "spring-cloud-config-server". I just delete this config: org.springframework.cloud spring-cloud-config-server

It sounds like your project's folder structure is wrong.

Code should go under src/main/java and resources (like your javascript) should go under src/main/resources . You have a few different options where you can actually serve the files from. This post on the spring.io blog has the following to say:

Spring Boot will automatically add static web resources located within any of the following directories:

  • /META-INF/resources/
  • /resources/
  • /static/
  • /public/

Another option you also have is using webjars .

Personally, I've found it easiest to put those kind of files under src/main/resources/public . It always works without any issues for me. The interesting thing is you can put a folder named /public anywhere in your project and spring-boot will serve files out of it. You have to be really careful that it's under src/main/resources/public though if you're using a build tool like maven, as when you come to build your .jar the files won't be in the right place otherwise.

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