春季靴 - 是否可以禁用终点
假设我有一个控制器:
public class MyController {
public String endpoint1() {...}
public String endpoint2() {...}
}
我想在春季出于任何原因禁用endpoint1
。简而言之,只是禁用它,以免它访问。因此,我不是在寻找在这种情况下返回的响应或如何确保该端点的响应。只是想简单地禁用端点,例如@disabled
注释左右。
解决方案更新:
感谢所有贡献的人。我决定提出@adolink建议。但是,该解决方案只会禁用对控制器的访问,从而未找到404。但是,如果您使用OpenAPI,则您的控制器及其所有模型(例如请求/响应主体)仍将在Swagger中显示出来。
因此,除了Adolin的建议外,还向我的控制器添加了@hidden OpenAPI注释,例如:
in application.properties ,设置:
cars.controller.enabled=false
然后在您的控制器中,使用它。要从OpenAPI/Swagger隐藏控制器,您可以使用@hiden
tag:
@Hidden
@ConditionalOnExpression("${cars.controller.enabled}")
@RestController
@RequestMapping("/cars")
public class Carontroller {
...
}
此后,该控制器处理的每个端点都将返回404,并且OpenAPI/Swagger不会显示控制器,也不会显示控制器其任何相关的模式对象,例如carrequestModel,carresponsemodel等。
Assuming I have a controller like:
public class MyController {
public String endpoint1() {...}
public String endpoint2() {...}
}
I want to disable endpoint1
for whatever reason in Spring. Simply, just disable it so that it cannot be accessed. So, I am not looking for how and what response to return in that case or how to secure that endpoint. Just looking to simply disable the endpoint, something like @Disabled
annotation on it or so.
SOLUTION UPDATE:
Thanks all who contributed. I decided to go with @AdolinK suggestion . However, that solution will only disable access to the controller resulting into 404 Not Found. However, if you use OpenApi, your controller and all of its models such as request/response body will still show in swagger.
So, in addition to Adolin's suggestion and also added @Hidden OpenApi annotation to my controllers like:
In application.properties, set:
cars.controller.enabled=false
Then in your controller, use it. To hide controller from the OpenApi/Swagger as well, you can use @Hiden
tag:
@Hidden
@ConditionalOnExpression("${cars.controller.enabled}")
@RestController
@RequestMapping("/cars")
public class Carontroller {
...
}
After this, every end point handled by this controller will return 404 Not Found and OpenApi/Swagger will not show the controllers nor any of its related schema objects such as CarRequestModel, CarResponseModel etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用@conditionalonexpression注释。
在application.properties中,您指出默认情况下启用控制器的contrenter
artivealOnexpression设置了false属性,并且不允许访问端点
You can use @ConditionalOnExpression annotation.
In application.properties, you indicates that controller is enabled by default
ConditionalOnExpression sets false your property, and doesn't allow access to end-point
为什么不删除该方法上的映射注释?
Why not remove the mapping annotation over that method?
尝试这种简单的方法:您可以定义属性
is.enable.enpoint1
以灵活的方式打开/关闭端点。如果关闭端点,请返回
404
或错误
页面,这取决于您的情况。Try this simple approach: You can define a property
is.enable.enpoint1
to turn on/off your endpoint in a flexible way.If you turn off the endpoint, then return a
404
orerror
page, which depends on your situation.