404 error redirect in Spring MVC with java config -


i have simple spring mvc application want hande 404 not found exceptions in advice controller class

configuration:

   public class appinitializer extends abstractannotationconfigdispatcherservletinitializer {       @override     protected class<?>[] getservletconfigclasses() {         return new class<?>[]{webconfig.class};     }       @override     protected class<?>[] getrootconfigclasses() {         return new class<?>[]{rootconfig.class, securityconfig.class};     }       @override     protected string[] getservletmappings() {         return new string[]{"/"};     }       @override     public void onstartup(servletcontext servletcontext) throws servletexception {         super.onstartup(servletcontext);         servletcontext.addlistener(new sessionlistener());         filterregistration.dynamic encodingfilter = servletcontext.addfilter("encodingfilter", new characterencodingfilter());         encodingfilter.setinitparameter("encoding", "utf-8");         encodingfilter.setinitparameter("forceencoding", "true");         encodingfilter.addmappingforurlpatterns(null, true, "/*");     } } 

controller:

@controller @requestmapping("/") public class homecontroller {  @requestmapping(value = "/error/", method = requestmethod.get)     public string error(){return "error";} } 

controlleradvice:

@controlleradvice public class advicecontroller {      @exceptionhandler(myownexception.class)     @responsestatus(value= httpstatus.bad_request)     public string checkoutexception(checkoutexception e, httpservletrequest httpservletrequest)   {                 return "error";     } } 

i can catch own exceptions when manually throw myownexception can't how catch nohandlerfound exception. need send 404 error code , appropriate error.jsp page when there no controller method handle request

if webapp using web.xml it's simple - add following (assuming usage of internalresourceviewresolver prefix pointing @ web-inf view folder , suffix .jsp). can have multiple error-page elements of other error codes too.

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

if not using web.xml it's more complicated , you'll have define , register own exceptionresolver. take @ spring.io blog article details on how this.


(edit after comment)

if want catch nohandlerfound exception first have tell spring throw via setting flag in dispatcherservlet directly. so, in appinitializer class add dispatcherservlet definition on top of doing add 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 set 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 can catch nohandlerfound exception directly in advicecontroller:

@controlleradvice public class advicecontroller {     //..     @exceptionhandler(nohandlerfoundexception.class)     public string dealwithnohandlerfoundexception(checkoutexception e, httpservletrequest httpservletrequest)   {                 return "error";     } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -