Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot custom error page for 404

Tags:

spring-boot

i wish to have custom 404 error page .I have velocity in my classpath but I don't want to use velocity view resolver Below is my code

@EnableAutoConfiguration(exclude={VelocityAutoConfiguration.class})
@ComponentScan
public class Application {
Properties props = new Properties();

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}
}

i am not able to redirect all the 404 to my some html in resource directory.

Please assist

P.S It work if i am use velocityresolver and have error.vm in template directory.

Thanks ans regards

like image 944
Abhishek kapoor Avatar asked Oct 06 '14 10:10

Abhishek kapoor


People also ask

How do I create a 404 page in spring boot?

We first need to create a custom HTML error page. If we save this file in resources/templates directory, it'll automatically be picked up by the default Spring Boot's BasicErrorController. We can be more specific by naming the file with the HTTP status code we want it used e.g. saving the file as 404.

How do I fix a WhiteLabel error page in spring boot?

enabled to false . Another way of disabling the WhiteLabel Error is excluding the ErrorMvcAutoConfiguration . Alternatively, the exclusion can be done in an annotation. When the WhiteLabel Error Page is disabled and no custom error page is provided, the web server's error page (Tomcat, Jetty) is shown.


1 Answers

You can provide a EmbeddedServletContainerCustomizer and add your (e.g. static) error pages there. See Error Handling in the spring boot docs. E.g:

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html"));
            }
        };
}
like image 170
cfrick Avatar answered Nov 09 '22 09:11

cfrick