Getting 404 when trying to access js and css files in Java Spring

63 views Asked by At

I use spring boot with thymeleaf and I am trying to link js file to my template.

In my html file I reference my scrpit file as such:

<script th:src="@{/js/file.min.js}" charset="UTF-8" type="text/javascript"></script>

The file is located in /resources/static/js

I am using Spring Security 6.1.5, Spring Boot 3.1.5, and Atlassian Connect Spring Boot 4.0.5.

I tried the following:

Adding spring-mvc.xml with content:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <mvc:resources mapping="/js/**" location="/js/" />

</beans>

Tried adding a configuration such as:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new RequestsInterceptor());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Configure resource handling for your static resources
        registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
    }
}

Also I tried adding some settings in my application.yml file

spring:
  web: 
    resources:
      add-mappings: true
      static-locations: classpath:/static/, classpath:/public/, classpath:/resources/, classpath:/META-INF/resources/, classpath:/js/

none of these solutions worked

0

There are 0 answers