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