My linux world » Java Spring – Exception

Java Spring - Exception


Contents

Prerequistes

Please read : Java spring – quickstart

Configuration

WebMvcConfig.java

You can add the following Bean. This will redirect all ‘java.lang.Exception’ in the ‘/error’ viewer :

  @Bean(name="simpleMappingExceptionResolver")
  public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() {
    SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
 
    Properties mappings = new Properties();
    mappings.setProperty("java.lang.Exception", "/error");
    simpleMappingExceptionResolver.setExceptionMappings(mappings);
    return simpleMappingExceptionResolver;
  }

error.jsp

${exception} is added to the context.

You can print the exception message :

${exception.message}

And the stack strace :

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 
<b>Stack trace:</b><br>
<ul>
  <c:forEach var="entry" items="${exception.stackTrace}" >
   <li>${entry}</li>
  </c:forEach>
</ul>

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.