SpringMVC 异常处理
2015-06-18 22:07
337 查看
Using HTTP Status Codes 在我们自定义的异常上使用ResponseStatus注解。当我们的Controller抛出异常,并且没有被处理的时候,他将返回HTTP STATUS 为指定值的 HTTP RESPONSE。这在我们写REST API的时候经常用到,比如: @ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such Order") // 404 public class OrderNotFoundException extends
RuntimeException { // ... } 我们的Controller为: @RequestMapping(value="/orders/{id}", method=GET) public String showOrder(@PathVariable("id") long id, Model model) { Order order = orderRepository.findOrderById(id); if (order == null) throw new OrderNotFoundException(id);
model.addAttribute(order); return "orderDetail"; } 这时候会返回404,转到404错误页面 Controller Based Exception Handling 在一个Controller中,,注意这种只在单个Controller中有效。这么做可以: 发生异常后,改变Response status,一般而言,发生异常返回HTTP STATUS 500.我们可以变为其他。发生错误后转到错误页面可以为不同异常定义不同处理(如不同的错误页面,不同的Response
status)举例说明 @Controller public class ExceptionHandlingController { // 我们标注了@RequestMapping的方法 ... //处理异常的方法。 // 把我们定义的异常转换为特定的Http status code @ResponseStatus(value=HttpStatus.CONFLICT, reason="Data integrity violation") // 409 @ExceptionHandler(DataIntegrityViolationException.class)
public void conflict() { // Nothing to do } // 捕获到SQLException,DataAccessException异常之后,转到特定的页面。 @ExceptionHandler({SQLException.class,DataAccessException.class}) public String databaseError() { //仅仅转到错误页面,我们在页面上得不到这个Exception的值,要得到值,我们可以通过下面的方法得到 return "databaseError";
} // 通过ModelAndView返回页面,以及往页面传相应的值 @ExceptionHandler(Exception.class) public ModelAndView handleError(HttpServletRequest req, Exception exception) { logger.error("Request: " + req.getRequestURL() + " raised " + exception); ModelAndView mav = new ModelAndView();
mav.addObject("exception", exception); mav.addObject("url", req.getRequestURL()); mav.setViewName("error"); return mav; } } Global Exception Handling 在类上使用 @ControllerAdvice注解,可以使得我们处理整个程序中抛出的异常。。举例: class GlobalControllerExceptionHandler { @ResponseStatus(HttpStatus.CONFLICT)
// 409 @ExceptionHandler(DataIntegrityViolationException.class) public void handleConflict() { // Nothing to do } //转到特定页面 。。。。。 } 如果我们要处理程序中所有的异常可以这么做: @ControllerAdvice class GlobalDefaultExceptionHandler { public static final String DEFAULT_ERROR_VIEW =
"error"; @ExceptionHandler(value = Exception.class) public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { // If the exception is annotated with @ResponseStatus rethrow it and let // the framework handle it - like the
OrderNotFoundException example // at the start of this post. // AnnotationUtils is a Spring Framework utility class. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { throw e; } // Otherwise setup and send the user to a default
error-view. ModelAndView mav = new ModelAndView(); mav.addObject("exception", e); mav.addObject("url", req.getRequestURL()); mav.setViewName(DEFAULT_ERROR_VIEW); return mav; } } Going Deeper 实现HandlerExceptionResolver接口,SpringMvc可以使用他来处理Controller中抛出的异常 public
interface HandlerExceptionResolver { ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex); } SpringMvc使用三种默认的HandlerExceptionResolver来处理我们的异常 ExceptionHandlerExceptionResolver:。 ResponseStatusExceptionResolver:
DefaultHandlerExceptionResolver:把Spring定义的一些标准异常,转换为HTTP STATUS CODE. Spring内置的SimpleMappingExceptionResolver实现了HandlerExceptionResolver接口,也是我们经常使用的,XML配置如下:
分类: java, Spring
RuntimeException { // ... } 我们的Controller为: @RequestMapping(value="/orders/{id}", method=GET) public String showOrder(@PathVariable("id") long id, Model model) { Order order = orderRepository.findOrderById(id); if (order == null) throw new OrderNotFoundException(id);
model.addAttribute(order); return "orderDetail"; } 这时候会返回404,转到404错误页面 Controller Based Exception Handling 在一个Controller中,,注意这种只在单个Controller中有效。这么做可以: 发生异常后,改变Response status,一般而言,发生异常返回HTTP STATUS 500.我们可以变为其他。发生错误后转到错误页面可以为不同异常定义不同处理(如不同的错误页面,不同的Response
status)举例说明 @Controller public class ExceptionHandlingController { // 我们标注了@RequestMapping的方法 ... //处理异常的方法。 // 把我们定义的异常转换为特定的Http status code @ResponseStatus(value=HttpStatus.CONFLICT, reason="Data integrity violation") // 409 @ExceptionHandler(DataIntegrityViolationException.class)
public void conflict() { // Nothing to do } // 捕获到SQLException,DataAccessException异常之后,转到特定的页面。 @ExceptionHandler({SQLException.class,DataAccessException.class}) public String databaseError() { //仅仅转到错误页面,我们在页面上得不到这个Exception的值,要得到值,我们可以通过下面的方法得到 return "databaseError";
} // 通过ModelAndView返回页面,以及往页面传相应的值 @ExceptionHandler(Exception.class) public ModelAndView handleError(HttpServletRequest req, Exception exception) { logger.error("Request: " + req.getRequestURL() + " raised " + exception); ModelAndView mav = new ModelAndView();
mav.addObject("exception", exception); mav.addObject("url", req.getRequestURL()); mav.setViewName("error"); return mav; } } Global Exception Handling 在类上使用 @ControllerAdvice注解,可以使得我们处理整个程序中抛出的异常。。举例: class GlobalControllerExceptionHandler { @ResponseStatus(HttpStatus.CONFLICT)
// 409 @ExceptionHandler(DataIntegrityViolationException.class) public void handleConflict() { // Nothing to do } //转到特定页面 。。。。。 } 如果我们要处理程序中所有的异常可以这么做: @ControllerAdvice class GlobalDefaultExceptionHandler { public static final String DEFAULT_ERROR_VIEW =
"error"; @ExceptionHandler(value = Exception.class) public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { // If the exception is annotated with @ResponseStatus rethrow it and let // the framework handle it - like the
OrderNotFoundException example // at the start of this post. // AnnotationUtils is a Spring Framework utility class. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { throw e; } // Otherwise setup and send the user to a default
error-view. ModelAndView mav = new ModelAndView(); mav.addObject("exception", e); mav.addObject("url", req.getRequestURL()); mav.setViewName(DEFAULT_ERROR_VIEW); return mav; } } Going Deeper 实现HandlerExceptionResolver接口,SpringMvc可以使用他来处理Controller中抛出的异常 public
interface HandlerExceptionResolver { ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex); } SpringMvc使用三种默认的HandlerExceptionResolver来处理我们的异常 ExceptionHandlerExceptionResolver:。 ResponseStatusExceptionResolver:
DefaultHandlerExceptionResolver:把Spring定义的一些标准异常,转换为HTTP STATUS CODE. Spring内置的SimpleMappingExceptionResolver实现了HandlerExceptionResolver接口,也是我们经常使用的,XML配置如下:
分类: java, Spring
相关文章推荐
- Java分割字符串函数split的用法
- Java学习指南学习笔记
- Eclipse + genymotion 搭建Android开发环境并调试
- java中随机生成随机数及不重复的随机数字
- Spring MVC 静态文件访问
- Java并发编程-27-异常处理及取消任务
- Struts2中迭代器的使用
- 最受欢迎的eclipse插件汇总
- java集合类
- javabean总结
- 探究java IO之AutoCloseable,Closeable和Flushable接口
- java实现ftp上传下载
- 20150618-Java项目心得1(里面内容均摘自我的有道云笔记)
- MyEclipse10安装SVN插件
- 卡片游戏-程序设计实例(Java)
- win7下JDK环境变量设置方法
- Spring技术内幕——Spring Framework的IOC容器实现(四)
- java基础 第6章类再生
- 在Java中如何把char数组转换为字符串
- Java-马士兵设计模式学习笔记-工厂模式-模拟Spring读取Properties文件