6

I need to serve static html file (/src/main/resources/static/folder/index.html) for all routes in specified root (as example '/main/\**'). I've tried to annotate controller method with @RequestMapping("/main/**"), but it works only for '/main' route, not for '/main/foo', '/main/foo/bar', etc...

So, how i can do this in spring boot?

2

2 Answers 2

6

I found this solution:

// application.properties
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.html


// Controller for index.html
@Controller
public class IndexController {

    @RequestMapping({"/login", "/main/**"})
    public String index() {
        return "index";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This worked for me, in addition just wanted to throw in I annotated the other main Application Class with @SpringBootApplication and put my static angular app in the resources\public folder
1

You have to add / edit a Configuration object.

Here is our way to do it:

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter {
    public static final String INDEX_VIEW_NAME = "forward:index.html";



    public void addViewControllers(final ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName(INDEX_VIEW_NAME);
        registry.addViewController("/login").setViewName(INDEX_VIEW_NAME);
        registry.addViewController("/logout").setViewName(INDEX_VIEW_NAME);
    }
}

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.