Spring MVC: How to Return Custom 404 Errorpages

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

Thymleaf 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:

  1. Do not disable the Whitelabel display from the properties file or from the main class.

  2. Define the error view resolver completely as in solution 3.

  3. The HTML pages must be in resources/templates

  4. 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



Leave a reply



Submit