spring基于注解的全局异常处理方式

http://snv.iteye.com/blog/2067065
需求:
系统运行有运行时异常,这些异常又不能呈现给客户,那么就需要进行全局处理
实现:
基于@ExceptionHandler
1.定义一个BaseController的所有的Controller都继承他
public class BaseController {  
    @ExceptionHandler({ RuntimeException.class })  
    public String exception(RuntimeException e) {  
        //do your biz  
        return "usr/tologin";  
    }  
} 

当有异常产生时定位到usr/tologin
2.所有的Controller都继承他
@Controller  
@RequestMapping("upload")  
public class UploadController extends BaseController {  
    private Logger log = LoggerFactory.getLogger(UploadController.class);  
  
    @RequestMapping(value = "/image", method = RequestMethod.POST, headers = "content-type=application/octet-stream")  
    public String image(HttpServletRequest request) throws Exception {  
         //you biz  
        return "image";  
    }  
}  

你可能感兴趣的:(spring)