Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 318
Jelly Avatar asked Apr 26 '26 09:04

Jelly


2 Answers

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">
like image 98
Lemmy Avatar answered Apr 28 '26 23:04

Lemmy


I was able to resolve issue in my project by using below path

href="${pageContext.request.contextPath}/resources/static/..."

so try using below path

href="${pageContext.request.contextPath}/resources/static/css/style.css"

There may be better way of resolving it but till then this is a quick fix.

like image 33
Shashank Avatar answered Apr 28 '26 23:04

Shashank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!