手机版
你好,游客 登录 注册
背景:
阅读新闻

SpringMVC 异常处理

[日期:2015-06-19] 来源:Linux社区  作者:hupengcool [字体: ]

Using HTTP Status Codes

在我们自定义的异常上使用ResponseStatus注解。当我们的Controller抛出异常,并且没有被处理的时候,他将返回HTTP STATUS 为指定值的 HTTP RESPONSE,比如:

    @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中有效。这么做可以:

  1. 发生异常后,改变Response status,一般而言,发生异常返回HTTP STATUS 500.我们可以变为其他。
  2. 发生错误后转到错误页面
  3. 可以为不同异常定义不同处理(如不同的错误页面,不同的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来处理我们的异常

  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.htm

SpringMVC详细示例实战教程 http://www.linuxidc.com/Linux/2015-06/118461.htm

Spring MVC 框架搭建及详解 http://www.linuxidc.com/Linux/2012-01/52740.htm

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-06/119049.htm

linux
相关资讯       SpringMVC  SpringMVC 异常处理 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款