0

I am a very beginner in spring. In my application, I created a global exception handler class using @ControllerAdvice. It is catching all 5xx errors. But when it is getting some 403 error, it is showing the default page.

My calls file is

package com.example.learn.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@ControllerAdvice
public class GlobalExceptionController {

 
    @ExceptionHandler(Exception.class)
    public ModelAndView exceptionHandler(Exception exception) {
        ModelAndView errorPage = new ModelAndView();
        errorPage.setViewName("error");
        errorPage.addObject("errormsg", exception.getMessage());
        return errorPage;
    }

}

And when it is getting some 403 error the below page is coming

enter image description here

How can I show a custom error page instead of this in the global exception handler?

Spring Version: 3.0.5

Java version: 17

Build using maven

Thanks in advance

Arun
  • 3,640
  • 7
  • 44
  • 87
  • I think this is not handled by the exception handler, but with access denied handler. See the example: https://www.baeldung.com/spring-security-custom-access-denied-page – Sergi Mar 24 '23 at 09:22

0 Answers0