Spring catch the whole route for index.html

I am developing a spring backend for a one-page reaction-based application in which I use a router agent for client-side routing.

In addition to the index.html page, the backend serves data along the path /api/** .

To serve my index.html from src/main/resources/public/index.html in the root path / my application, I added a resource handler

 @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/").addResourceLocations("/index.html"); } 

I want to use the index.html page whenever there is no other route, for example. when I call a path other than /api .

How to configure such catch-all route in spring?

+6
source share
4 answers

Since my response app can use the root as a direct target, this ultimately works for me

 @Configuration public class WebConfiguration extends WebMvcConfigurerAdapter { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/{spring:\\w+}") .setViewName("forward:/"); registry.addViewController("/**/{spring:\\w+}") .setViewName("forward:/"); registry.addViewController("/{spring:\\w+}/**{spring:?!(\\.js|\\.css)$}") .setViewName("forward:/"); } } 

Honestly, I have no idea why this should be in this particular format in order to avoid an endless loop of forwarding.

+9
source

Avoid @EnableWebMvc

By default, Spring-Boot executes static content in src/main/resources :

  • / META-INF / resources /
  • /resources/
  • /static/
  • /state/

Look this one and this one ;

Or save @EnableWebMvc and override addViewControllers

Did you specify @EnableWebMvc ? Take a look at this: Java Spring Download: how to map application root ("/") to index.html?

Either you delete @EnableWebMvc, or you can override addViewControllers :

 @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("forward:/index.html"); } 

Or define a controller to catch /

You can see this spring-boot-reactjs sample project on github :

It does what you want using the controller:

 @Controller public class HomeController { @RequestMapping(value = "/") public String index() { return "index"; } } 

Its index.html is under src/main/resources/templates

+5
source

Found an answer by looking at this question

 @Bean public EmbeddedServletContainerCustomizer notFoundCustomizer() { return new EmbeddedServletContainerCustomizer() { @Override public void customize(ConfigurableEmbeddedServletContainer container) { container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/")); } }; } 
+1
source

I use the react and react-router in my spring boot application, and it was as easy as creating a controller that has a mapping to / and subtrees of my site, like /users/** Here is my solution

 @Controller public class SinglePageAppController { @RequestMapping(value = {"/", "/users/**", "/campaigns/**"}) public String index() { return "index"; } } 

Api calls are not caught by this controller, and resources are processed automatically.

0
source

All Articles