gpt4 book ai didi

java - micronaut Java 中的全局异常处理

转载 作者:行者123 更新时间:2023-12-04 12:56:46 42 4
gpt4 key购买 nike

寻找更好的解决方案来处理 micronaut 中的全局异常 https://docs.micronaut.io/latest/guide/index.html#errorHandling
Controller

@Controller("/category")
public class CategoryController {
@Delete(uri = "/{id}")
public Maybe<HttpResponse> delete(@NotBlank String id) {
LOG.info(String.format("API --> Deleting the specified category"));
return iCategoryManager.Count(id).flatMap(item -> {
if (item > 0) {
iCategoryManager.Delete(id).subscribe();
return Maybe.just(HttpResponse.noContent());
} else
return Maybe.just(HttpResponse.notFound());
});
}
}
iCategoryManager.Count(id)导致如下异常,如何在 上捕获异常GlobalExceptionHandler
io.micronaut.core.serialize.exceptions.SerializationException: Incorrect message body size to deserialize to a Long
at io.micronaut.rabbitmq.serdes.JavaLangRabbitMessageSerDes$LongSerDes.deserialize(JavaLangRabbitMessageSerDes.java:314)
at io.micronaut.rabbitmq.serdes.JavaLangRabbitMessageSerDes$LongSerDes.deserialize(JavaLangRabbitMessageSerDes.java:306)
at io.micronaut.rabbitmq.serdes.JavaLangRabbitMessageSerDes.deserialize(JavaLangRabbitMessageSerDes.java:81)
at io.micronaut.rabbitmq.intercept.RabbitMQIntroductionAdvice.deserialize(RabbitMQIntroductionAdvice.java:323)
at io.micronaut.rabbitmq.intercept.RabbitMQIntroductionAdvice.lambda$intercept$22(RabbitMQIntroductionAdvice.java:268)
at io.reactivex.internal.operators.flowable.FlowableFlatMap$MergeSubscriber.onNext(FlowableFlatMap.java:132)
at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onNext(RxInstrumentedSubscriber.java:59)
at io.reactivex.internal.operators.flowable.FlowableTimeoutTimed$TimeoutSubscriber.onNext(FlowableTimeoutTimed.java:101)
at io.micronaut.reactive.rxjava2.RxInstrumentedSubscriber.onNext(RxInstrumentedSubscriber.java:59)
at io.reactivex.internal.subscriptions.DeferredScalarSubscription.complete(DeferredScalarSubscription.java:132)
at io.reactivex.internal.operators.single.SingleToFlowable$SingleToFlowableObserver.onSuccess(SingleToFlowable.java:62)
at io.micronaut.reactive.rxjava2.RxInstrumentedSingleObserver.onSuccess(RxInstrumentedSingleObserver.java:65)
at io.reactivex.internal.operators.single.SingleFlatMap$SingleFlatMapCallback$FlatMapSingleObserver.onSuccess(SingleFlatMap.java:111)
at io.micronaut.reactive.rxjava2.RxInstrumentedSingleObserver.onSuccess(RxInstrumentedSingleObserver.java:65)
at io.reactivex.internal.operators.single.SingleDoFinally$DoFinallyObserver.onSuccess(SingleDoFinally.java:73)
at io.micronaut.reactive.rxjava2.RxInstrumentedSingleObserver.onSuccess(RxInstrumentedSingleObserver.java:65)
at io.reactivex.internal.operators.single.SingleCreate$Emitter.onSuccess(SingleCreate.java:67)
at io.micronaut.rabbitmq.reactive.RxJavaReactivePublisher$3.handleDelivery(RxJavaReactivePublisher.java:324)
at com.rabbitmq.client.impl.ConsumerDispatcher$5.run(ConsumerDispatcher.java:149)
at com.rabbitmq.client.impl.ConsumerWorkService$WorkPoolRunnable.run(ConsumerWorkService.java:104)
全局异常处理
@Produces
@Singleton
@Requires(classes = {GlobalException.class, ExceptionHandler.class})
public class GlobalExceptionHandler implements ExceptionHandler<GlobalException, HttpResponse> {

@Override
public HttpResponse handle(HttpRequest request, GlobalException exception) {
return HttpResponse.ok(0);
}
}

public class GlobalException extends RuntimeException{
}
我如何 Hook GlobalExceptionHandler。应用程序上发生的任何异常都应在 处捕获。 GlobalExceptionHandler

最佳答案

当服务器发生异常时 ExceptionHandler在与抛出的异常最匹配的 bean 上下文中查找(通过处理程序的类型参数)。要处理所有异常,需要替换所有提供的异常处理程序。大约有十几个。
一种选择是替换默认的错误响应处理器。例如,这就是导致 body 成为 HATEOS 的原因。见 documentation关于具体的错误处理。

关于java - micronaut Java 中的全局异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65747492/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com