Spring custom loading error page for 404

I want to create a custom page with error 404. I have speed in my classpath, but I do not want to use a converter with a speed of viewing 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 cannot redirect all 404 to my html directory in the resource directory.

Please, help

PS This works if I use speedresolver and have error.vm in the templates directory.

Thank you and welcome

+5
source share
2 answers

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

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

As of Spring Download 1.4.x :

If you want to display the HTML custom error page for this status code, you can add the file to the /error folder. Error pages can be static HTML (that is, added to any of the static resource directories) or created using templates. The file name must be an exact status code (for example, 404 ) or a series mask (for example, 5xx ).

For example, to map 404 to a static HTML file, your folder structure would look like this:

 src/ +- main/ +- java/ | + <source code> +- resources/ +- public/ +- error/ | +- 404.html +- <other public assets> 

To map all 5xx errors using the freemarker template, you have this structure:

 src/ +- main/ +- java/ | + <source code> +- resources/ +- template/ +- error/ | +- 5xx.ftl +- <other templates> 

Check out the Spring Download Documentation for a more in-depth discussion.

+8
source

Source: https://habr.com/ru/post/1204096/


All Articles