SpringMVC异常处理-实例展示2(全局异常处理)




项目结构

pom.xml


    4.0.0
    com.mvc
    spring-mvc-exception-handler
    0.0.1-SNAPSHOT
    war
    
        
            org.springframework
            spring-webmvc
            4.3.9.RELEASE
        
        
            javax.servlet
            servlet-api
            2.5
            provided
        
    

web.xml



    spring-mvc-exception-handler
    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring-*.xml
        
        1
    
    
    
        DispatcherServlet
        *.do
    

spring-mvc.xml



    
    
    
    
        
        
        
    

UserController.java

package com.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user/")
public class UserController {
    @RequestMapping("update")
    @ResponseBody
    public String update(Integer id){
        System.out.println("update user");
        if(id==null)
            throw new NullPointerException("id can not be null");
        return "update ok!";
        
    }
    @RequestMapping("findByUserName")
    @ResponseBody
    public String findByUserName(String userName){
        System.out.println("find user by userName");
        if(userName==null)
            throw new RuntimeException("userName can not be null");
        return userName;
        
    }
    /**
     * 定义Controller中的异常处理方法
     * 借助ExceptionHandler注解进行描述
     * 注解中的内容表示这个方法能够处理的异常类型(包括这个异常的子类类型)
     * @param e
     * @return
     */
    @ExceptionHandler(value=NullPointerException.class)
    @ResponseBody
    public String handleException(Exception e){
        System.out.println("局部异常处理");
        System.out.println("UserController.handleException()");
        return e.getMessage();
    }
}

GolbalExceptionHandler.java

package com.mvc.exception.controller;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * 使用@ControllerAdvice注解描述的类
 * 一般要做为全局的异常处理类
 */
@ControllerAdvice
public class GolbalExceptionHandler {
    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public String handleException(Throwable e){
        System.out.println("全局异常处理。。。");
        System.out.println("GolbalExceptionHandler.handleException()");
        return e.getMessage();
    }
}

http://localhost/spring-mvc-exception-handler/user/findByUserName.do

find user by userName
全局异常处理。。。
GolbalExceptionHandler.handleException()

http://localhost/spring-mvc-exception-handler/user/update.do

update user
局部异常处理
UserController.handleException()

注意

  • 使用@ControllerAdvice注解描述的类,一般要做为全局的异常处理类

你可能感兴趣的:(SpringMVC异常处理-实例展示2(全局异常处理))