1

In Spring Boot <2 I would do:

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/404").setViewName("redirect:/not-found");
    }

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return container -> container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));
    }
}

But EmbeddedServletContainerCustomizer is now missing, I have tried:

    @Bean
    public ConfigurableServletWebServerFactory webServerFactory() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404"));

        return factory;
    }

but with no luck.

Alex Parvan
  • 413
  • 1
  • 4
  • 16

1 Answers1

1

Nevermind, that WAS the actual answer, I forgot to add the error page as a route.

@Controller
public class RouterController {

    @RequestMapping({
            "/error"
    })
    public String index() {
        return "forward:/index.html";
    }
}

Hopefully this will help, that's the way to add a custom error page in Spring Boot 2 with Angular.

Alex Parvan
  • 413
  • 1
  • 4
  • 16
  • Could you provide a complete answer ? – Stephane Apr 28 '18 at 15:41
  • I added the Router configuration. Hope it helps. – Alex Parvan Apr 30 '18 at 05:43
  • for me this throws: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'basicErrorController' method – Kovács Ede May 26 '18 at 07:20
  • see this here https://stackoverflow.com/questions/25356781/spring-boot-remove-whitelabel-error-page basically u need to implement spring built in ErrorController, would be nice to improve the answer, in order to share a copy paste solution for future folks :) //@Controller public class IndexController implements ErrorController { private static final String PATH = "/error"; //@RequestMapping(value = PATH) public String error() { return "forward:/index.html"; } @Override public String getErrorPath() { return PATH; } } – Kovács Ede May 26 '18 at 10:23