2 Ekim 2023 Pazartesi

SpringMVC ErrorController Arayüzü - Custom ErrorController

Giriş
Şu satırı dahil ederiz
import org.springframework.boot.autoconfigure.web.ErrorController;
Açıklaması şöyle
The WhiteLabel error handling system is built using the ErrorController, which is a part of the Spring Boot framework. By default, this function is taken over by the BasicErrorController, which is responsible for the white-label error-handling features.
Örnek
Şöyle yaparız
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CustomErrorController implements ErrorController {

  @RequestMapping("/error")
  public String handleError() {
    // Your custom error handling logic goes here
    return "error"; // Return the name of your custom error page
  }

  public String getErrorPath() {
    return "/error";
  }
}
src/main/resources/public
src/main/resources/static
src/main/resources/templates
dizinlerinden birisinde error.html dosyası yaratırız

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <title>Error Page</title>
</head>
<body>
   <h1>Oops! Something went wrong.</h1>
   <p>We're sorry, but an error occurred.</p>
</body>
</html>


Hiç yorum yok:

Yorum Gönder