How to create your own Controller corresponding to / error with Spring Boot

It was necessary to log an unexpected exception, so I investigated how to create a self-made Controller corresponding to / error with Spring Boot.

It is possible if you follow the URL below. https://www.logicbig.com/tutorials/spring-framework/spring-boot/implementing-error-controller.html

Copy the source so that the URL is broken.

python


@Controller
public class MyCustomErrorController implements ErrorController {

  @RequestMapping("/error")
  @ResponseBody
  public String handleError(HttpServletRequest request) {
      Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
      Exception exception = (Exception) request.getAttribute("javax.servlet.error.exception");
      return String.format("<html><body><h2>Error Page</h2><div>Status code: <b>%s</b></div>"
                      + "<div>Exception Message: <b>%s</b></div><body></html>",
              statusCode, exception==null? "N/A": exception.getMessage());
  }

  @Override
  public String getErrorPath() {
      return "/error";
  }
}

There is a lot of information on API exception handling, but this information has not come out easily. ..

Recommended Posts

How to create your own Controller corresponding to / error with Spring Boot
Create your own Utility with Thymeleaf with Spring Boot
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to use built-in h2db with spring boot
[Spring Boot Actuator] How to manually register your own health check process
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
How to boot by environment with Spring Boot of Maven
Create microservices with Spring Boot
Create an app with Spring Boot 2
How to set Spring Boot + PostgreSQL
Create an app with Spring Boot
How to use ModelMapper (Spring boot)
Sample code to unit test a Spring Boot controller with MockMvc
Create your own validator with Bean Validation
How to split Spring Boot message file
How to apply thymeleaf changes to the browser immediately with #Spring Boot + maven
How to read Body of Request multiple times with Spring Boot + Spring Security
How to create a server executable JAR and WAR with Spring gradle
How to create your own headless API using Liferay's REST Builder (Part 4)
How to create your own headless API using Liferay's REST Builder (Part 1)
How to create your own annotation in Java and get the value
Create a website with Spring Boot + Gradle (jdk1.8.x)
Create a simple search app with Spring Boot
How to use Spring Boot session attributes (@SessionAttributes)
Try to implement login function with Spring Boot
How to create multiple pull-down menus with ActiveHash
How to add a classpath in Spring Boot
Create CRUD apps with Spring Boot 2 + Thymeleaf + MyBatis
How to give your image to someone with docker
Add your own authentication items with Spring Security
How to bind to property file in Spring Boot
Try to automate migration with Spring Boot Flyway
[Java] Article to add validation with Spring Boot 2.3.1.
I wanted to gradle spring boot with multi-project
Create Spring Boot environment with Windows + VS Code
How to create member variables with JPA Model
Create a web api server with spring boot
[Spring Boot] How to refer to the property file
[Introduction to Spring Boot] Authentication function with Spring Security
Spring Boot --How to set session timeout time
Create a Spring Boot development environment with docker
Create Spring Cloud Config Server with security with Spring Boot 2.0
How to perform UT with Excel as test data with Spring Boot + JUnit5 + DBUnit
How to create an Excel form using a template file with Spring MVC
Part2 Part II. How to proceed with Getting Started Spring Boot Reference Guide Note ①
Settings for connecting to MySQL with Spring Boot + Spring JDBC
How to set Dependency Injection (DI) for Spring Boot
How to write a unit test for Spring Boot 2
Automatically map DTOs to entities with Spring Boot API
[Java] How to omit spring constructor injection with Lombok
Customize REST API error response with Spring Boot (Part 2)
Extend Spring Boot DefaultErrorViewResolver to dynamically customize error screens
How to use CommandLineRunner in Spring Batch of Spring Boot
Create Restapi with Spring Boot ((1) Until Run of App)
How to create docker-compose
Create a simple demo site with Spring Security with Spring Boot 2.1
Customize REST API error response with Spring Boot (Part 1)
Attempt to SSR Vue.js with Spring Boot and GraalJS
Call your own method with PreAuthorize in Spring Security
Download with Spring Boot