Skip to content
Advertisement

No mapping for static-resources Spring Boot

In my Spring-Boot application, js and css files do not work, it says 404 not found.

My html-page includes the following:

  <head>  
     <link href="/webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
     <link href="/static/css/style.css" rel="stylesheet">
     <script src="/webjars/jquery/jquery.min.js"></script>
     <script src="/webjars/sockjs-client/sockjs.min.js"></script>
     <script src="/webjars/stomp-websocket/stomp.min.js"></script>
     <script src="/static/js/operations.js"></script>
 </head>

I configured resources so:

 @Configuration
 @EnableWebMvc
 public class MvcConfig implements WebMvcConfigurer {
     @Override
     public void addResourceHandlers(ResourceHandlerRegistry registry) {
         registry
               .addResourceHandler("/resources/**")
               .addResourceLocations("/resources/");
        }
  }

But in logs I receive:

  o.s.web.servlet.PageNotFound             : No mapping for GET /favicon.ico
  o.s.web.servlet.PageNotFound             : No mapping for GET /favicon.ico
  o.s.web.servlet.PageNotFound             : No mapping for GET /webjars/jquery/jquery.min.js
  o.s.web.servlet.PageNotFound             : No mapping for GET /static/css/style.css
  o.s.web.servlet.PageNotFound             : No mapping for GET /static/js/operations.js
  o.s.web.servlet.PageNotFound             : No mapping for GET /favicon.ico

This is the location of static-sources:

sources

What am I doing wrong?

Advertisement

Answer

By default, this handler serves static content from any of /static, /public, /resources, and /META-INF/resources directories that are on the classpath. Since src/main/resources is typically on the classpath by default, we can place any of these directories there.

This means that your links should look like:

<link href="/css/style.css" rel="stylesheet">

instead of

<link href="/static/css/style.css" rel="stylesheet">

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement