gpt4 book ai didi

java - 我可以在 spring 中使用 afterThrowing() 方法返回自定义类型的对象吗?

转载 作者:行者123 更新时间:2023-12-01 09:50:06 25 4
gpt4 key购买 nike

我遇到了一种情况,我想模拟 Web 服务调用。我使用的方法是,如果服务关闭,它会抛出异常。我打算在 spring 中使用 ThrowAdvice AOP 来使用该异常,并希望抑制该异常并用我的虚拟对象替换响应对象。我怎样才能这样做?

最佳答案

我建议你使用Around Advisory来拦截该方法。这个建议会给你绝对的控制权。在此建议中,您可以捕获异常并返回 require 响应

@Around("execution(abc.example.*Service.*(..))")
public ResultType execute(ProceedingJoinPoint p) {
ResultType result = null;
try {
result = (ResultType) p.proceed();
return result;
} catch (Throwable t) {}
// Create dummy result
return result;
}

ResultType 可以是任何类型,但一定不要拦截未返回相同类型或子类型的服务

关于java - 我可以在 spring 中使用 afterThrowing() 方法返回自定义类型的对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37651526/

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