Error Page In Spring
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings spring error handling and policies of this site About Us Learn more about Stack Overflow
Whitelabel Error Page Spring Boot
the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags custom error page spring mvc Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only
Spring Boot Error Page Filter
takes a minute: Sign up Spring MVC: How to return custom 404 errorpages? up vote 19 down vote favorite 5 I'm looking for a clean way to return customized 404 errorpages in Spring 4 when a requested resource was not found. Queries to different domain types should result in different error pages. Here some code to show my intention (Meter spring 404 error page is a domain class): @RequestMapping(value = "/{number}", method = RequestMethod.GET) public String getMeterDetails(@PathVariable("number") final Long number, final Model model) { final Meter result = meterService.findOne(number); if (result == null) { // here some code to return an errorpage } model.addAttribute("meter", result); return "meters/details"; } I imagine several ways for handling the problem. First there would be the possibility to create RuntimeExceptions like @ResponseStatus(HttpStatus.NOT_FOUND) public class MeterNotFoundExcption extends RuntimeException { } and then use an exception handler to render a custom errorpage (maybe containing a link to a list of meters or whatever is appropriate). But I don't like polluting my application with many small exceptions. Another possibility would be using HttpServletResponse and set the statuscode manually: @RequestMapping(value = "/{number}", method = RequestMethod.GET) public String getMeterDetails(@PathVariable("number") final Long number, final Model model, final HttpServletResponse response) { final Meter meter = meterService.findOne(number); if (meter == null) { response.setStatus(HttpStatus.NOT_FOUND.value()); return "meters/notfound"; } model.addAttribute("meter", meter); return "meters/details"; } But with this solution I have to duplicate the first 5 lines for many controller methods (like edit, delete). Is there an elegant way to pre
here for a quick overview of the
Exception Handling In Spring Mvc 4
site Help Center Detailed answers to any questions you
Spring Boot Custom Error Page
might have Meta Discuss the workings and policies of this site About Us spring mvc exception handling best practices Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation http://stackoverflow.com/questions/21061638/spring-mvc-how-to-return-custom-404-errorpages Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up 404 error page configuration with Spring 4 annotations up http://stackoverflow.com/questions/32324630/404-error-page-configuration-with-spring-4-annotations vote 1 down vote favorite I have a Spring MVC project with Spring 4. My server is tomcat 7. I'm trying to make a 404 page, I tried many things, but I couldn't do that. What am I missing? Here is WebAppContext : @Configuration @ComponentScan(basePackages = { "com.***" }) @EnableWebMvc public class WebAppContext extends WebMvcConfigurerAdapter { private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/pages/"; private static final String VIEW_RESOLVER_SUFFIX = ".jsp"; @Autowired private EventLogService eventLogService; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public SimpleMappingExceptionResolver exceptionResolver() { SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver(); Properties exceptionMappings = new Properties(); exceptionMappings.put("org.springframework.security.web.authentication.rememberme.CookieTheftException", "user/login?error=sessionExpired"); exceptionMappings.put("java.lang.RuntimeException", "error/error"); exceptionMappings.put("java.lang.Exception", "error/error"); exceptionResolver.setExceptionMappings(exceptionMappings); Properties statusCodes = new Properties(); statusCodes.put("error/403", "403"); statusCodes.put("error/404", "404"); statusCodes.put("error/error", "500"); exceptionResolver.setStatusCodes(statusCodes); return exceptionResolver; } @Bean public ViewResolver viewResolver() { Int
a GitHub account Sign in Create a gist now https://gist.github.com/jonikarppinen/662c38fb57a23de61c8b Instantly share code, notes, and snippets. Star 20 Fork 4 jonikarppinen/CustomErrorController.java Last active Oct 13, 2016 Embed What would you like to http://ankushs92.github.io/tutorial/2015/11/01/custom-error-pages-with-spring-boot.html do? Embed Embed this gist in your website. Embed Share Copy sharable URL for this gist. Share Clone via HTTPS Clone with error page Git or checkout with SVN using the repository's web address. HTTPS Learn more about clone URLs Download ZIP Code Revisions 5 Stars 20 Forks 4 Example of replacing Spring Boot "whitelabel" error page with custom error responses (with JSON response body) Raw CustomErrorController.java package com.company.project.controllers; error page spring import com.company.project.model.api.ErrorJson; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.web.ErrorAttributes; import org.springframework.boot.autoconfigure.web.ErrorController; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map; /** * Based on the helpful answer at http://stackoverflow.com/q/25356781/56285, * with error details in response body added. * * @author Joni Karppinen * @since 20.2.2015 */ @RestController public class CustomErrorController implements ErrorController { private static final String PATH = "/error"; @Value("${debug}") private boolean debug; @Autowired private ErrorAttributes errorAttributes; @RequestMapping(value = PATH) ErrorJson error(HttpServletRequest request, HttpServletResponse response) { // Appropriate HTTP response code (e.g. 404 or 500) is automatically set by Spring. // Here we just define response body. return new ErrorJson(response.getStatus(), getErrorAttributes(request, debug)); } @Override public String getErrorPath() { return PATH; } private Map
a passion , and have been using Spring MVC for a while or deciding which framework to use for your next project , then you are in for a treat. Spring Boot is a great web framework based on the philosophy of convention over configuration , backed by the goodness of the entire Spring ecosystem ,and does not mandate a single line of XML. How about that ! One of the regular requirement for any website / web app is to built custom error pages. These pages can be plain white pages that only show the error message with its Http error code (which is what are going to do ) or some fancy page displaying the error message. Here is the directory structure : Consider the following snippet : @SpringBootApplication public class SampleBootErrorPagesApplication { private static final String PATH = "/errors"; public static void main(String[] args) { SpringApplication.run(SampleBootErrorPagesApplication.class, args); } @Bean public EmbeddedServletContainerCustomizer containerCustomize