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?

Pavya
  • 6,015
  • 4
  • 29
  • 42
Anton
  • 575
  • 2
  • 7
  • 27
  • It is done by adding a view controller, as discussed here: http://stackoverflow.com/questions/27381781/java-spring-boot-how-to-map-my-app-root-to-index-html. according to the spring javadoc documentation https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.html#addViewController-java.lang.String- you can either map from a concrete path or from a pattern, which is what you need. – Yonatan Wilkof Apr 15 '16 at 05:57
  • thats odd, it seems to be working for me – Manish Kothari Apr 15 '16 at 06:08

2 Answers2

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";
    }
}
Anton
  • 575
  • 2
  • 7
  • 27
  • 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 – xpagesbeast Dec 12 '18 at 17:34
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);
    }
}
WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52