3

I'm learning spring boot recently and I use angular as the frontend. I'm tired of the thymeleaf, so I try to load the html5 without the module (I put the html, css and js in the package resource/static). I can load the index.html only when I enter the specific URL http://localhost:8080/index.html instead of the http://localhost:8080/.

Here is my project structure

How can I directly get the index with http://localhost:8080/?

3 Answers 3

2

Just add a controller which serves index.html on /:

@GetMapping("/")
public String index() {
    return "/resources/static/index.html";
}
Sign up to request clarification or add additional context in comments.

Comments

0

My issue was that I had the @EnableWebMvc tag in a different class and this didn't allow my index.html to be returned from resources/static when accessing localhost:8080

1 Comment

hi same issue here, may i know how you solve it in 2023 ?
0

If you use "HTML5 style navigation" in Angular (normal URLs, without the '#' character in the middle), you can use a Spring MVC Functional Endpoint to redirect all 'text/html' requests to 'index.html':

@Bean
RouterFunction<ServerResponse> angularRouter(@Value("classpath:/static/index.html") 
                                             Resource indexHtml) {
    return route(method(HttpMethod.GET)
                 .and(accept(MediaType.TEXT_HTML))
                 .and(pathExtension(Objects::isNull)),
            request -> ServerResponse.ok()
                       .contentType(MediaType.TEXT_HTML)
                       .body(indexHtml));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.