在我們自定義的異常上使用ResponseStatus注解。當(dāng)我們的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,轉(zhuǎn)到404頁面而不是錯誤頁面
Controller Based Exception Handling在一個Controller中,,注意這種只在單個Controller中有效。這么做可以:
舉例說明
@Controllerpublic class ExceptionHandlingController { // 我們標(biāo)注了@RequestMapping的方法 ... //處理異常的方法。 // 把我們定義的異常轉(zhuǎn)換為特定的Http status code @ResponseStatus(value=HttpStatus.CONFLICT, reason="Data integrity violation") // 409 @ExceptionHandler(DataIntegrityViolationException.class) public void conflict() { // Nothing to do } // 捕獲到SQLException,DataaccessException異常之后,轉(zhuǎn)到特定的頁面。 @ExceptionHandler({SQLException.class,DataAccessException.class}) public String databaseError() { //僅僅轉(zhuǎn)到錯誤頁面,我們在頁面上得不到這個Exception的值,要得到值,我們可以通過下面的方法得到 return "databaseError"; } // 通過ModelAndView返回頁面,以及往頁面?zhèn)飨鄳?yīng)的值 @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 } //轉(zhuǎn)到特定頁面 。。。。。}
如果我們要處理程序中所有的異常可以這么做:
@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實(shí)現(xiàn)HandlerExceptionResolver接口,SpringMvc可以使用他來處理Controller中拋出的異常
public interface HandlerExceptionResolver { ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex);}
SpringMvc使用三種默認(rèn)的HandlerExceptionResolver來處理我們的異常
Spring內(nèi)置的SimpleMappingExceptionResolver實(shí)現(xiàn)了HandlerExceptionResolver接口,也是我們經(jīng)常使用的,xml配置如下:
<bean id="simpleMappingExceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <map> <!-- key為異常類型,value為要轉(zhuǎn)到的頁面 --> <entry key="DatabaseException" value="databaseError"/> <entry key="InvalidCreditCardException" value="creditCardError"/> </map> </property> <!-- 默認(rèn)的異常頁面 --> <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異常信息,默認(rèn)不設(shè)置-不記錄異常信息 --> <property name="warnLogCategory" value="example.MvcLogger"/> </bean>
新聞熱點(diǎn)
疑難解答
圖片精選