404 Error Redirect in Spring with Java Config

404 error redirect in Spring MVC with java config

If your webapp is using web.xml it's very simple - just add the following (assuming usage of InternalResourceViewResolver with prefix pointing at your WEB-INF view folder and suffix .jsp). You can have multiple error-page elements of other error codes too.

<error-page>
<error-code>404</error-code>
<location>/error</location>
</error-page>

If you are not using web.xml it's more complicated and you'll have to define and register your own ExceptionResolver. Take a look at this spring.io blog article for details on how to do this.


(Edit after comment)

If you want to catch the NoHandlerFound exception you first have to tell Spring to throw it via setting a flag in the DispatcherServlet directly. To do so, in your AppInitializer class add the DispatcherServlet definition on top of what you are currently doing to add the flag:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new SessionListener());
//BEGIN OF NEW CODE
WebApplicationContext context = getContext();
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
//we did all this to set the below flag
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet",dispatcherServlet );
//END OF NEW CODE
FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encodingFilter", new CharacterEncodingFilter());
encodingFilter.setInitParameter("encoding", "UTF-8");
encodingFilter.setInitParameter("forceEncoding", "true");
encodingFilter.addMappingForUrlPatterns(null, true, "/*");
}

Then you can catch the NoHandlerFound exception directly in your AdviceController:

@ControllerAdvice
public class AdviceController {
//..
@ExceptionHandler(NoHandlerFoundException.class)
public String dealWithNoHandlerFoundException(CheckoutException e, HttpServletRequest httpServletRequest) {
return "error";
}
}

404 error in Spring (java config / no web.xml)

Although not as clear as I would like, this is a sort of working version to at least provide some customisation to error pages. It is a first approach but hopefully can help others.

The list of handled exceptions is not extensive, but mainly addressing 404 errors (NoHandlerFoundException) and other typical errors like InternalServerErrorException and NullPointerException, to try to catch them all in the end with a generic error for everything else that is an Exception).

Note that this is not covering other exceptions related to e.g. bad syntax in a JSTL template (org.apache.jasper.*; exceptions that cannot apparently be caught here).

These are the related changes and additions to the source base:

CustomSimpleMappingExceptionResolver.java (provide generic exceptions, yet logs details)

package ...;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import javax.ws.rs.InternalServerErrorException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;

public class CustomSimpleMappingExceptionResolver extends SimpleMappingExceptionResolver {

public CustomSimpleMappingExceptionResolver() {
// Turn logging on by default
setWarnLogCategory(getClass().getName());
}

@Override
public String buildLogMessage(Exception e, HttpServletRequest req) {
return "MVC exception: " + e.getLocalizedMessage();
}

@Override
protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response,
Object handler, Exception ex) {

// Log exception
ex.printStackTrace();
String exceptionCause = ex.toString();
String exceptionType = ex.getClass().getCanonicalName();

// Get the ModelAndView to use
ModelAndView mav = super.doResolveException(request, response, handler, ex);

// Make more information available to the view - note that SimpleMappingExceptionResolver adds the exception already
mav.addObject("url", request.getRequestURL());
mav.addObject("timestamp", new Date());

ArrayList<String> exceptions404 = new ArrayList<String>(
Arrays.asList(
NoHandlerFoundException.class.getName()
)
);
ArrayList<String> exceptions500 = new ArrayList<String>(
Arrays.asList(
InternalServerErrorException.class.getName(),
NullPointerException.class.getName()
)
);

String userExceptionDetail = ex.toString();
String errorHuman = "";
String errorTech = "";

if (exceptions404.contains(exceptionType)) {
errorHuman = "We cannot find the page you are looking for";
errorTech = "Page not found";
userExceptionDetail = String.format("The page %s cannot be found", request.getRequestURL());
mav.setViewName("/error/404");
mav.addObject("status", HttpStatus.NOT_FOUND.value());
} else if (exceptions500.contains(exceptionType)) {
errorHuman = "We cannot currently serve the page you request";
errorTech = "Internal error";
userExceptionDetail = "The current page refuses to load due to an internal error";
mav.setViewName("/error/500");
mav.addObject("status", HttpStatus.INTERNAL_SERVER_ERROR.value());
} else {
errorHuman = "We cannot serve the current page";
errorTech = "General error";
userExceptionDetail = "A generic error prevents from serving the page";
mav.setViewName("/error/generic");
mav.addObject("status", response.getStatus());
}

Exception userException = new Exception(userExceptionDetail);
mav.addObject("error_human", errorHuman);
mav.addObject("error_tech", errorTech);
mav.addObject("exception", userException);
return mav;
}
}

WebAppConfig.java (registers custom exception resolver as an exception handler)

package ...;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.env.Environment;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.lang.ClassNotFoundException;
import java.lang.NullPointerException;
import javax.annotation.Resource;
import javax.ws.rs.InternalServerErrorException;
import java.util.Properties;

@Configuration
@ComponentScan("...")
@EnableWebMvc
@EnableTransactionManagement
@PropertySource("classpath:application.properties")
public class WebAppConfig extends WebMvcConfigurerAdapter {

@Resource
private Environment env;

// ...

@Bean
HandlerExceptionResolver customExceptionResolver () {
CustomSimpleMappingExceptionResolver resolver = new CustomSimpleMappingExceptionResolver();
Properties mappings = new Properties();
// Mapping Spring internal error NoHandlerFoundException to a view name
mappings.setProperty(NoHandlerFoundException.class.getName(), "/error/404");
mappings.setProperty(InternalServerErrorException.class.getName(), "/error/500");
mappings.setProperty(NullPointerException.class.getName(), "/error/500");
mappings.setProperty(ClassNotFoundException.class.getName(), "/error/500");
mappings.setProperty(Exception.class.getName(), "/error/generic");
resolver.setExceptionMappings(mappings);
// Set specific HTTP codes
resolver.addStatusCode("404", HttpStatus.NOT_FOUND.value());
resolver.addStatusCode("500", HttpStatus.INTERNAL_SERVER_ERROR.value());
resolver.setDefaultErrorView("/error/generic");
resolver.setDefaultStatusCode(200);
// This resolver will be processed before the default ones
resolver.setOrder(Ordered.HIGHEST_PRECEDENCE);
resolver.setExceptionAttribute("exception");
return resolver;
}

// ...

@Bean
public InternalResourceViewResolver setupViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
return resolver;
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
}
}

Initializer.java (adding dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);; maybe not needed)

package ...;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class Initializer implements WebApplicationInitializer {

public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebAppConfig.class);
servletContext.addListener(new ContextLoaderListener(ctx));
ctx.setServletContext(servletContext);
DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx);
dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);

// Add the dispatcher servlet mapping manually and make it initialize automatically
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", dispatcherServlet);
servlet.addMapping("/");
servlet.addMapping("*.png");
servlet.addMapping("*.jpg");
servlet.addMapping("*.css");
servlet.addMapping("*.js");
servlet.addMapping("*.txt");
servlet.setLoadOnStartup(1);

// ...

}
}

Structure of views and tags related to error classes:

    src/main/webapp/WEB-INF/
├── tags
│   └── error.tag
└── views
├── error
│ ├── 404.jsp
│ ├── 500.jsp
└────── generic.jsp

src/main/webapp/WEB-INF/tags/error.tag

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<head>
<title>Error page</title>
</head>
<body>
<div class="container">
<h3><c:out value="${error_human}" /></h3>

<p><br/><br/></p>

<div class="panel panel-primary">
<div class="panel-heading">
<c:out value="${error_tech}" />
</div>
<div class="panel-body">
<p><c:out value="${exception_message}" /></p>
</div>
</div>
</div>
</body>
</html>

src/main/webapp/WEB-INF/views/error/404.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isErrorPage="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib tagdir="/WEB-INF/tags/" prefix="g" %>

<c:set var = "error_human" scope = "session" value = "We cannot find the page you are looking for"/>
<c:set var = "error_tech" scope = "session" value = "Page not found"/>
<c:set var = "exception_message" scope = "session" value = "The current page cannot be found"/>
<g:error />

src/main/webapp/WEB-INF/views/error/500.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isErrorPage="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib tagdir="/WEB-INF/tags/" prefix="g" %>

<c:set var = "error_human" scope = "session" value = "We cannot currently serve the page you request"/>
<c:set var = "error_tech" scope = "session" value = "Internal error"/>
<c:set var = "exception_message" scope = "session" value = "The current page refuses to load due to an internal error"/>
<g:error />

src/main/webapp/WEB-INF/views/error/generic.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isErrorPage="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib tagdir="/WEB-INF/tags/" prefix="g" %>

<c:set var = "error_human" scope = "session" value = "We cannot serve the current page"/>
<c:set var = "error_tech" scope = "session" value = "General error"/>
<c:set var = "exception_message" scope = "session" value = "A generic error prevents from serving the page"/>
<g:error />

Configure spring boot to redirect 404 to a single page app

This should do the trick: Add an error page for 404 that routes to /notFound, and forward that to your SPA (assuming the entry is on /index.html):

@Configuration
public class WebApplicationConfig extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/notFound").setViewName("forward:/index.html");
}

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return container -> {
container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,
"/notFound"));
};
}

}

Redirect to a URL with 404 status code in Java

I have solved this by using below code.

Response.sendError(HttpServletResponse.SC_NOT_FOUND);

which sends 404 to front end for the requested url. At application level, we have a configuration where if 404 code receives redirect to a user friendly page.

Spring MVC: How to return custom 404 errorpages?

The solution is much simpler than thought. One can use one generic ResourceNotFoundException defined as follows:

public class ResourceNotFoundException extends RuntimeException { }

then one can handle errors within every controller with an ExceptionHandler annotation:

class MeterController {
// ...
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String handleResourceNotFoundException() {
return "meters/notfound";
}

// ...

@RequestMapping(value = "/{number}/edit", method = RequestMethod.GET)
public String viewEdit(@PathVariable("number") final Meter meter,
final Model model) {
if (meter == null) throw new ResourceNotFoundException();

model.addAttribute("meter", meter);
return "meters/edit";
}
}

Every controller can define its own ExceptionHandler for the ResourceNotFoundException.



Related Topics



Leave a reply



Submit