栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring应用程序中的异常处理位置

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring应用程序中的异常处理位置

这是在Spring中开始异常处理的好方法:

步骤1- 创建一个特定的DefaultExceptionHandler类,并使用 @ControllerAdvice
批注对其进行批注。在此处理程序类中,您可以使用不同的方法来捕获 预期的 异常和 意外的 异常,这些异常使用
@ExceptionHandler 注释进行了注释:

@ControllerAdvice("com.stackoverflow.example")@SuppressWarnings("WeakerAccess")public class DefaultExceptionHandler extends ResponseEntityExceptionHandler {    private final Logger log = LoggerFactory.getLogger("DefaultExceptionHandler");    private final MessageSourceAccessor messageSource;    @Autowired    public DefaultExceptionHandler(MessageSourceAccessor messageSource) {        Assert.notNull(messageSource, "messageSource must not be null");        this.messageSource = messageSource;     }      @ExceptionHandler(ApplicationSpecificException.class)      public ResponseEntity<Object> handleApplicationSpecificException(ApplicationSpecificExceptionex) {         final Error error = buildError(ex);         return handleExceptionInternal(ex, ex.getHttpStatus(), error);      }       @ExceptionHandler(Exception.class)       public ResponseEntity<Object> handleException(Exception ex) {final Error error = buildError(ex);return handleExceptionInternal(ex, HttpStatus.INTERNAL_SERVER_ERROR, error);    }}

第2步-
创建用于预期异常的特定于应用程序的异常(ApplicationSpecificException类),并将此异常置于任何级别,Spring会对其进行拾取:

public class ApplicationSpecificException extends RuntimeException {    private static final long serialVersionUID = 1L;    private final ExceptionType exceptionType;    public ApplicationSpecificException(ExceptionType exceptionType, Object... messageArguments) {        super(MessageFormat.format(exceptionType.getMessage(), messageArguments));        this.exceptionType = exceptionType;    }    public ApplicationSpecificException(ExceptionType exceptionType, final Throwable cause, Object... messageArguments) {        super(MessageFormat.format(exceptionType.getMessage(), messageArguments), cause);        this.exceptionType = exceptionType;    }    public HttpStatus getHttpStatus() {        return exceptionType.getStatus();    }    public ExceptionType getExceptionType() {        return exceptionType;    }}

以ExceptionType为枚举:

public enum ExceptionType {    HTTP_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "An internal server error occurred.");    //you can specify your own exception types...    private HttpStatus status;    private String message;    ExceptionType(HttpStatus status, String message) {        this.status = status;        this.message = message;    }    public HttpStatus getStatus() {        return status;    }    public String getMessage() {        return message;    }}

步骤3- 最后,创建一个ExceptionFactory类。这使您可以在应用程序日志中自动记录异常:

public class ExceptionFactory {    private static final Logger LOG = LoggerFactory.getLogger(ExceptionFactory.class);    public static ApplicationSpecificException create(final Throwable cause, final ExceptionType exceptionType, final Object... messageArguments) {        LOG.error(MessageFormat.format(exceptionType.getMessage(), messageArguments), cause);        return new ApplicationSpecificException (exceptionType, cause, messageArguments);    }    public static ApplicationSpecificException create(final ExceptionType exceptionType, final Object... messageArguments) {        LOG.error(MessageFormat.format(exceptionType.getMessage(), messageArguments));        return new TerminologyServerException(exceptionType, messageArguments);    }}

步骤4- 现在,您可以在应用程序中的任何位置抛出异常,这会将异常记录在应用程序日志中。借助于Spring
@ControllerAdvice批注,DefaultExceptionHandler抛出并拾取了此异常:

throw ExceptionFactory.create(ExceptionType.INTERNAL_SERVER_ERROR);

像这样,您将应对异常处理过程作为一个跨领域的问题。内部服务器错误不会传播到最终用户,并且DefaultExceptionHandler会处理预期的异常和意外的异常。为该异常分配了某个HTTP错误代码和错误消息,这些错误代码和错误消息将返回给客户端。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/615482.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号