Please make sure to include thymeleaf in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Add these in your application.properties
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
spring.thymeleaf.check-template-location=true
The following is optional. You might need this, for example, if you want to change the path to store static files.
spring.web.resources.static-locations=classpath:/your/path/to/the/*folder*/that/has/index/file
spring.thymeleaf.prefix=classpath:/your/path/to/the/*folder*/that/has/index/file
Create your SpaController.java then write like this:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SpaController {
@RequestMapping(value = {"/{path:^(?!api|public)[^\\.]*}", "/**/{path:^(?!api|public).*}/{path:[^\\.]*}"})
public String get(){
return "index";
}
}
Or, in Kotlin:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class SpaController {
@RequestMapping(value = [
"/{path:^(?!api|public)[^\\.]*}",
"/**/{path:^(?!api|public).*}/{path:[^\\.]*}"
])
public fun get() : String
{
return "index";
}
}
But please note that this might not work for path like /***/***/***
(having slashes more than 2). If you need that case, you need to add or modify the matcher.
See also: Spring catch all route for index.html
NoHandlerFoundException
doesn't even get fired. Server just throwsInsufficientAuthenticationException
. And when I simply replaced thePageNotFoundController
's@ExceptionHandler
annotation with that exception, it didn't seem to work. Server returned a 401 Unauthorized white label page. Do you have a suggestion for that ? – Ignition