Exception Handling in Spring MVC
详细地介绍了Spring MVC中的异常处理。
Per Exception
@ResponseStatus(value=HttpStatus.NOT_FOUND, reason="No such Order")
public class OrderNotFoundException extends RuntimeException { }
Per Controller
@Controller
public class ExceptionHandlingController {
@ResponseStatus(value=HttpStatus.CONFLICT,
reason="Data integrity violation") // 409
@ExceptionHandler(DataIntegrityViolationException.class)
public void conflict() {
}
}
Globally
可以用@ControllerAdvice
和HandlerExceptionResolver
。