I use spring MVC in my web application. I have been trying various options to return custom error pages for the various exceptions thrown in my application.
I have managed to do that using the @ControllerAdvice
annotation. My global exception handler class would be as follow:
import org.apache.velocity.exception.ResourceNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class ExceptionControllerAdvice
{
private static final Logger logger = LoggerFactory.getLogger(ExceptionControllerAdvice.class);
@ExceptionHandler(Exception.class)
public String exception(Exception e)
{
logger.error(e.toString());
return "exceptionPage";
}
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseBody
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleMissingParameter()
{
return "exceptionPage";
}
}
But the trouble I am facing is with the HTTP 404 error. Is there any way I can handle the HTTP status errors also using this annotation. I also use Apache tiles and I render my pages using tiles and I use ftl pages.