spring mvc异常的处理

1、全局处理

<!-- 总错误处理 -->
    <bean id="exceptionResolver"
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        <property name="defaultErrorView">
            <value>/error</value>
        </property>
        <property name="defaultStatusCode">
            <value>500</value>
        </property>
        <property name="warnLogCategory">
            <value>org.springframework.web.servlet.handler.SimpleMappingExceptionResolver
            </value>
        </property>
    </bean>

2、controller级别的

@RequestMapping("/exception")
public void ExceptionTest() throws Exception{
       throw new Exception("i don't know");
} 

@ExceptionHandler
public String handleException(Exception e,HttpServletRequest request){
       System.out.println(e.getMessage());
       return "helloworld";
}

访问:访问/exception会抛出一个异常,而handleException则抓到这个异常并进行处理 

原文地址:https://www.cnblogs.com/yangzhilong/p/3726750.html