Issue
I am unable to access my static html files in spring boot. I tried different configurations but still I get the resource not found error
Firstly I tried using JSP,it did not work, then I switched to thymeleaf it did work that is because of JPS limitations and finally I used html.
My controllers were working fine I used RestController and Controller for seperate test runs they worked thymeleaf required RequestMapping to be declared at the class level to work but in case of html I was stuck and then I found this
spring.mvc.view.prefix: /resources/**
spring.web.resources.chain.strategy.content.enabled=true
spring.web.resources.chain.strategy.content.paths=/**
spring.mvc.contentnegotiation.favor-parameter=true
I was able to access the static resources but without controller , they did not work with the controller and I am getting resource not found error.
And for your kind information I tried placing the html files in main/webapps/WEB-INF/, /resources/static/,/resources/templates/ and currently I am able to access them because they are in resources/static/ and I have an 404.html in /resources/public/error/404.html every time I use an endpoint I get the 404.html but the controllers are still not working
This is my controller
@Controller
public class AssignmentController {
@GetMapping("home")
public String home() {
return "home";
}
@GetMapping("/")
public String main() {
return "index";
}
@GetMapping("login")
public String myLogin() {
return "login";
}
}
And to be clear the url that works is http://localhost:8080/index.html/ which should be entered manually but when I use http://localhost:8080/home, http://localhost:8080/index, or http://localhost:8080/ I get the error page
And my last attempt was using webmvcconfigurer
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resourceViewResolver = new InternalResourceViewResolver();
resourceViewResolver.setPrefix("/resources/**/");
resourceViewResolver.setSuffix(".html");
registry.viewResolver(resourceViewResolver);
}
Here is the log
DEBUG 12920 --- [nio-8080-exec-9] o.s.w.servlet.view.InternalResourceView : View name 'login', model {}
DEBUG 12920 --- [nio-8080-exec-9] o.s.w.servlet.view.InternalResourceView : Forwarding to [/resources/**/login.html]
DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/resources/**/login.html", parameters={}
DEBUG 12920 --- [nio-8080-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
DEBUG 12920 --- [nio-8080-exec-9] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404
DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
DEBUG 12920 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
DEBUG 12920 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
I am using spring boot 3.1.5, my pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sunbasedata.qa2</groupId>
<artifactId>sunbase</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sunbase</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/foodbox
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.sql.init.mode=NEVER
spring.jpa.hibernate.ddl-auto=update
with a basic application like this the issue can be reproduce when you use @Controller and place your html files in the resources/static/ folder
Solution
I think you have a misconfiguration in your project. Serve static files are the most simple thing.
I tried your github sample (disabling the database because no body has your database) and the html files are available
Anyway I show you a thymeleaf sample
Thymeleaf
I started an old template with thymeleaf
https://github.com/jrichardsz/spring-boot-templates/tree/master/013-thymeleaf-sqlite-crud
I added a foo.txt (../src/main/resources/static/foo.txt) in the static folder
After the restart the file is ready:
Answered By - JRichardsz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.