gpt4 book ai didi

java - spring中如何动态禁用特定API?

转载 作者:行者123 更新时间:2023-11-30 05:25:23 26 4
gpt4 key购买 nike

我有一个标志 DISABLE_FLAG,我想用它来控制不同 Controller 中的多个特定 API。

@RestController
public final class Controller1 {
@RequestMapping(value = "/foo1", method = RequestMethod.POST)
public String foo1()
}
@RestController
public final class Controller2 {
@RequestMapping(value = "/foo2", method = RequestMethod.POST)
public String foo2()
}

我可以使用拦截器来处理所有的网址。有没有像注释这样简单的方法来做到这一点?

最佳答案

你可以使用 AOP 来做类似的事情。

创建您自己的注释...

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Maybe { }

以及相应的方面...

@Aspect
public class MaybeAspect {

@Pointcut("@annotation(com.example.Maybe)")
public void callMeMaybe() {}

@Around("callMeMaybe()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// do your logic here..
if(DISABLE_FOO) {
// do nothing ? throw exception?
// return null;
throw new IllegalStateException();
} else {
// process the request normally
return joinPoint.proceed();
}
}
}

关于java - spring中如何动态禁用特定API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58773615/

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