どのように私は、カスタム注釈の周りにAOPにメッセージをエラーことができますか?

ジュン:

私は、アノテーションの周りのAOPでのカスタムエラーメッセージにしたいです。私は前に@RestControllerAdviceを使用するために使用されるが、それは方法の周りにAOPでは動作しませんでした。それは、デフォルトのエラーメッセージを出力します。

私は〜キャッチ私はそれが//// 1または//// 2のような奇妙なことを知っているの試みを入力メッセージに試してみました

しかし、私はポイントを取得することはできません:(

TransactionAspectクラス

    @Around("execution(* com.bono.server.controller..*(..))")
    @Transactional
    public Object caculatePerformanceTime(ProceedingJoinPoint proceedingJoinPoint) {
        Object result = null;
        try {
            result = proceedingJoinPoint.proceed();
        } catch (CustomeException e) { ////// 1
            throw new ErrorMessage(CustomError.HTTP_400_MISTYPE);
        }
        catch (Throwable throwable) { /////// 2
            return new ErrorMessage(CustomError.HTTP_400_MISTYPE);
        }
        return result;
    }

ErrorMessageクラス

@Getter
@Setter
public class ErrorMessage {

    private int errorCode;
    private String errorMessage;

    public ErrorMessage(CustomError customError) {
        this.errorCode = customError.errorCode();
        this.errorMessage = customError.errorMessage();
    }
}

GroupExceptionAdviceクラス

@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GroupExceptionAdvice extends ResponseEntityExceptionHandler {

    //// 1
    @ExceptionHandler(value = CustomeException.class)
    public ResponseEntity<CustomErrorResponse> customhandleNotSatisfied(Exception ex, WebRequest request) {
        CustomErrorResponse error = new CustomErrorResponse();
        error.setTimestamp(LocalDateTime.now());
        error.setError(ex.getMessage());
        error.setStatus(HttpStatus.NOT_FOUND.value());

        return new ResponseEntity<>(error, HttpStatus.NOT_FOUND);
    }

    //// 2
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = CustomException.class)
    public ErrorMessage handlerUnResolvedAddressException(MisTypingException e) {
        return new ErrorMessage(CustomError.HTTP_400_MISTYPE);
    }
}
{
    "timestamp": "2019-08-12T01:14:16.467+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "class com.bono.server.config.exception.ErrorMessage cannot be cast to class org.springframework.http.ResponseEntity (com.bono.server.config.exception.ErrorMessage and org.springframework.http.ResponseEntity are in unnamed module of loader 'app')",
    "path": "/bono/api/alarm"
}

私はこのように表示したいです

{
    "code" : 102,
    "message" : "got it !"
}
kriegaex:

OPは、彼らが自分の問題を解決することを言ったので答えに私の以前のコメントを変換します。

あなたのErrorMessageクラスは、いずれも拡張していないExceptionThrowable、それでは、どのようにそれを投げることができますか?コードはコンパイルしてのようなコンパイルエラーが生成してもいけません。

No exception of type ErrorMessage can be thrown;
an exception type must be a subclass of Throwable

私はあなたのような何かを書くべきあなたのサンプルクラスのすなわち

public class ErrorMessage extends Exception {
  // (...)
}

チェック例外のためか、

public class ErrorMessage extends RuntimeException {
  // (...)
}

非チェック例外のために。しかし、あなたのクラス定義はつまり、暗黙的にそれが直接拡張し、何を拡張しませんObject

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=230976&siteId=1