Handling Exceptions in Spring MVC with Logging

  • Post author:
  • Post category:Java / Spring

In your Spring servlet.xml, add a HandlerExceptionResolver.

	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.Exception">/error/default</prop>
			</props>
		</property>
		<property name="defaultErrorView" value="/error/default" />
		<property name="warnLogCategory" value="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" />
	</bean>

“/error/default” is the URL it will go to that will display an error message to the user. warnLogCategory is the logger category. Exceptions will be logged under the warning level and not the error level. The category value can be anything. Here we just set it to the full name of SimpleMappingExceptionResolver.

Spring HandlerExceptionResolver won’t handle errors in the view, so capture these using web.xml

	<error-page>
		<exception-type>java.lang.Exception</exception-type>
		<location>/error/default</location>
	</error-page>

This Post Has 3 Comments

  1. naveen

    I tried with property “warnLogCategory” for exception logging..

    No exception being logged in the log file.. Do we need to do specific configuration in log4u.properties file ?
    please suggest.

Comments are closed.