Spring MVC: How to return custom 404 errorpages?
The solution is much simpler than thought. One can use one generic ResourceNotFoundException
defined as follows:
public class ResourceNotFoundException extends RuntimeException { }
then one can handle errors within every controller with an ExceptionHandler
annotation:
class MeterController {
// ...
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {
return "meters/notfound";
}
// ...
@RequestMapping(value = "/{number}/edit", method = RequestMethod.GET)
public String viewEdit(@PathVariable("number") final Meter meter,
final Model model) {
if (meter == null) throw new ResourceNotFoundException();
model.addAttribute("meter", meter);
return "meters/edit";
}
}
Every controller can define its own ExceptionHandler
for the ResourceNotFoundException
.
Customized 404 error page in spring-boot
With Spring Boot & Spring MVC you can create an error folder under resources/public and place your customer error pages. Spring will pick them up.
src/
+- main/
+- java/
| + <source code>
+- resources/
+- public/
+- error/
| +- 404.html
+- <other public assets>
If you're not using Spring MVC you'll have to register the error pages by implementing your own error page registrar.
@Bean
public ErrorPageRegistrar errorPageRegistrar(){
return new MyErrorPageRegistrar();
}
private static class MyErrorPageRegistrar implements ErrorPageRegistrar {
// Register your error pages and url paths.
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
registry.addErrorPages(new ErrorPage(HttpStatus.BAD_REQUEST, "/400"));
}
}
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-error-handling-custom-error-pages
Spring Boot and custom 404 error page
You're using Thymeleaf, And Thymeleaf can handle error without a controller.
For a generic error page this Thymeleaf page need to be named as error.html
and should be placed under src/main/resources > templates > error.html
For specific error pages, you need to create files named as the http error code in a folder named error, like: src/main/resources/templates/error/404.html
.
Problem trying to display custom error pages with Spring Boot
SOLVED
The solution is as follows:
Do not disable the Whitelabel display from the properties file or from the main class.
Define the error view resolver completely as in solution 3.
The HTML pages must be in resources/templates
You must add the following dependency to your application's pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
I hope this helps for everybody who needs it.
catch 404 error spring boot to return custom html
There are many ways to achieve the custom error page. One easy way is using /resources/public/error/404.html
which is not desired in your case.
Option:1
Could you try configuring ControllerAdvice.
application.properties
spring.mvc.throw-exception-if-no-handler-found=true
----------------------
@ControllerAdvice
class YourApplicationExceptionHandler {
@ResponseStatus(HttpStatus.NOT_FOUND) // 404
@ExceptionHandler(NoHandlerFoundException.class)
public ModelAndView handleNoHandlerFoundException() {
return new ModelAndView("viewName");
}
}
Option 2:
If you have any other path defined for the error you can simply change the /error path. In your Controller define the corresponding path mapping and route to the custom view.
server.error.path=/yourcustomerrorpath
Option 3: Using ErrorController
@Controller
public class MyCustomErrorController implements ErrorController {
@RequestMapping("/error")
public String handleError() {
return "globalerrorview";
}
@Override
public String getErrorPath() {
return "/error";
}
}
Related Topics
JPA & Criteria API - Select Only Specific Columns
Retrofit 2.0 How to Get Deserialised Error Response.Body
Jaxb Creating Context and Marshallers Cost
Retrieve Java Annotation Attribute
What Is the Most Accurate Encoding Detector
Org.Postgresql.Util.Psqlexception: Fatal: Sorry, Too Many Clients Already
Is It Necessary to Close Each Nested Outputstream and Writer Separately
Why Never Change the Notifier in Receiving a Change Event
Java: How to Test on Array Equality
Why Integer.Max_Value + 1 == Integer.Min_Value
Eclipse - Debugger Doesn't Stop at Breakpoint
Java String.Indexof and Empty Strings
Permutation Algorithm Without Recursion? Java
Filling Combobox from Database by Using Hibernate in Java