Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / SpringMVC 异常处理

Using HTTP Status Codes

在我们自定义的异常上使用ResponseStatus注解。当我们的Controller抛出异常,并且没有被处理的时候,他将返回HTTP STATUS 为指定值的 HTTP RESPONSE,比如:@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such Order")// 404public 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中有效。这么做可以:
  1. 发生异常后,改变Response status,一般而言,发生异常返回HTTP STATUS 500.我们可以变为其他。
  2. 发生错误后转到错误页面
  3. 可以为不同异常定义不同处理(如不同的错误页面,不同的Response status)
举例说明@Controllerpublic 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} //转到特定页面 。。。。。}如果我们要处理程序中所有的异常可以这么做:@ControllerAdviceclass 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来处理我们的异常
  1. ExceptionHandlerExceptionResolver:
  2. ResponseStatusExceptionResolver:
  3. DefaultHandlerExceptionResolver:把Spring定义的一些标准异常,转换为HTTP STATUS CODE.
Spring内置的SimpleMappingExceptionResolver实现了HandlerExceptionResolver接口,也是我们经常使用的,XML配置如下:<bean id="simpleMappingExceptionResolver"class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"><property name="exceptionMappings"><map><!-- key为异常类型,value为要转到的页面 --><entry key="DatabaseException" value="databaseError"/><entry key="InvalidCreditCardException" value="creditCardError"/></map></property><!-- 默认的异常页面 --><property name="defaultErrorView" value="error"/><!-- 在页面我们可以通过ex拿到异常信息 --><property name="exceptionAttribute" value="ex"/><!-- Name of logger to use to log exceptions. Unset by default, so logging disabled --> <!-- log异常信息,默认不设置-不记录异常信息 --><property name="warnLogCategory" value="example.MvcLogger"/></bean>Spring MVC+Spring3+Hibernate4开发环境搭建 http://www.linuxidc.com/Linux/2013-07/87119.htm Spring MVC整合Freemarker基于注解方式 http://www.linuxidc.com/Linux/2013-02/79660.htm 基于注解的Spring MVC简单介绍 http://www.linuxidc.com/Linux/2012-02/54896.htmSpringMVC详细示例实战教程 http://www.linuxidc.com/Linux/2015-06/118461.htmSpring MVC 框架搭建及详解 http://www.linuxidc.com/Linux/2012-01/52740.htm本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-06/119049.htm