{"id":1586,"date":"2016-10-04T15:37:35","date_gmt":"2016-10-04T13:37:35","guid":{"rendered":"http:\/\/blog.rabahi.net\/?page_id=1586"},"modified":"2016-10-04T15:37:35","modified_gmt":"2016-10-04T13:37:35","slug":"java-spring-exception","status":"publish","type":"page","link":"https:\/\/blog.rabahi.net\/?page_id=1586","title":{"rendered":"Java Spring &#8211; Exception"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Prerequistes\"><span class=\"toc_number toc_depth_1\">1<\/span> Prerequistes<\/a><\/li><li><a href=\"#Configuration\"><span class=\"toc_number toc_depth_1\">2<\/span> Configuration<\/a><ul><li><a href=\"#WebMvcConfigjava\"><span class=\"toc_number toc_depth_2\">2.1<\/span> WebMvcConfig.java<\/a><\/li><li><a href=\"#errorjsp\"><span class=\"toc_number toc_depth_2\">2.2<\/span> error.jsp<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h1><span id=\"Prerequistes\">Prerequistes<\/span><\/h1>\n<p>Please read : <a href=\"?page_id=1549\">Java spring &#8211; quickstart<\/a><\/p>\n<h1><span id=\"Configuration\">Configuration<\/span><\/h1>\n<h2><span id=\"WebMvcConfigjava\">WebMvcConfig.java<\/span><\/h2>\n<p>You can add the following Bean. This will redirect all &#8216;java.lang.Exception&#8217; in the &#8216;\/error&#8217; viewer :<\/p>\n<pre lang=\"java\">\r\n  @Bean(name=\"simpleMappingExceptionResolver\")\r\n  public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() {\r\n    SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();\r\n\r\n    Properties mappings = new Properties();\r\n    mappings.setProperty(\"java.lang.Exception\", \"\/error\");\r\n    simpleMappingExceptionResolver.setExceptionMappings(mappings);\r\n    return simpleMappingExceptionResolver;\r\n  }\r\n<\/pre>\n<h2><span id=\"errorjsp\">error.jsp<\/span><\/h2>\n<p>${exception} is added to the context.<\/p>\n<p>You can print the exception message :<\/p>\n<pre lang=\"html\">\r\n${exception.message}\r\n<\/pre>\n<p>And the stack strace :<\/p>\n<pre lang=\"html\">\r\n<%@taglib uri=\"http:\/\/java.sun.com\/jsp\/jstl\/core\" prefix=\"c\"%>\r\n\r\n<b>Stack trace:<\/b><br>\r\n<ul>\r\n  <c:forEach var=\"entry\" items=\"${exception.stackTrace}\" >\r\n   <li>${entry}<\/li>\r\n  <\/c:forEach>\r\n<\/ul>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 Prerequistes2 Configuration2.1 WebMvcConfig.java2.2 error.jsp Prerequistes Please read : Java spring &#8211; quickstart Configuration WebMvcConfig.java You can add the following Bean. This will redirect all &#8216;java.lang.Exception&#8217; in the &#8216;\/error&#8217; viewer : @Bean(name=&#8221;simpleMappingExceptionResolver&#8221;) public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty(&#8220;java.lang.Exception&#8221;, &#8220;\/error&#8221;); simpleMappingExceptionResolver.setExceptionMappings(mappings); return simpleMappingExceptionResolver; } error.jsp ${exception} is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1547,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1586","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1586"}],"version-history":[{"count":1,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1586\/revisions"}],"predecessor-version":[{"id":1587,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1586\/revisions\/1587"}],"up":[{"embeddable":true,"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=\/wp\/v2\/pages\/1547"}],"wp:attachment":[{"href":"https:\/\/blog.rabahi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}