Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-boot 404 error in serving .js, .css files to html templates

I am using spring boot and was trying to host HTML pages that use angular js. My folder structure is :My Folder Structure

I referred to a lot of other question on SO and even spring.io guide on spring boot and what I understood was that we are supposed to keep our html templates in templates folder and all the .css, images, .js files in static folder. I have done the same, but whenever the page loads , only the html is rendered and browser get 404 for all .css and .js files.

I have include .css and .js in my HTML in the following manner :

<script src="/js/socket.io/socket.io.js"></script>
<script src="/js/moment.min.js"></script>
<script src="/js/demoApp.js"></script>

I tried all the combinations like : src="../static/js/socket.io/socket.io.js" or src="js/socket.io/socket.io.js" but always I am getting 404 for these files, in the browser.

I checked the spring boot logs and this is what it says :

2016-07-24 21:08:05 WARN  PageNotFound:1139 - No mapping found for HTTP request with URI [/js/demoApp.js] in DispatcherServlet with name 'dispatcherServlet'
2016-07-24 21:08:05 DEBUG DispatcherServlet:997 - Successfully completed request

I am unable to understand how to solve the issue, please help !!!

Note : I have not written view resolver code because according to my understanding spring boot automatically picks up static content from static folder. Please correct me if I am wrong.

Edit : Adding my pom file :

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j</artifactId>
        </dependency>
        <!--Alexa Dependencies -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.io</artifactId>
            <version>2.4</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.amazon.alexa</groupId>
            <artifactId>alexa-skills-kit</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.0.0</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-dynamodb</artifactId>
            <version>1.9.40</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <verbose>true</verbose>
                    <source>1.8</source>
                    <target>1.8</target>
                    <showWarnings>true</showWarnings>
                </configuration>
            </plugin>
        </plugins>
    </build>
like image 964
sensitive_piece_of_horseflesh Avatar asked Feb 22 '26 22:02

sensitive_piece_of_horseflesh


1 Answers

I see you are using thymeleaf so try to access your resources like this :

<script th:src="@{/js/socket.io/socket.io.js}"></script>
<script th:src="@{/js/moment.min.js}"></script>
<script th:src="@{/js/demoApp.js}"></script>

Also and if this does not work can you add your index.html.

like image 177
Lazar Lazarov Avatar answered Feb 24 '26 11:02

Lazar Lazarov



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!