根据配置文件忽略摇摇欲坠的URL

发布于 2025-02-10 15:45:32 字数 150 浏览 2 评论 0原文

我想根据Spring Boot应用程序中的活动配置文件忽略Swagger URL。我找到了@apiignore和@hidden,但是无论配置文件如何,它们都隐藏了API。在这里,我想隐藏一些API仅适用于产品,而不是其他配置文件(即,开发,UAT等)。有什么办法可以实现这一目标。请建议

I want to ignore the swagger URL's based on active profiles in spring boot application. I found @ApiIgnore and @Hidden, but they hide API irrespective of profile. Here, I want to hide some API's only for PROD but not other profile(i.e., DEV, UAT, etc.,). Is there a way I can achieve this. Please suggest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2025-02-17 15:45:32

它们是多种方法,一种简单的方法(但根本不是优雅)实际上是为不同的配置文件创建多个案例。

例子:

@Configuration
public class SpringFoxConfig {

    @Bean
    @Profile("dev")
    public Docket getDocketForDev() {
        final Set<String> produces = new HashSet<String>();
        produces.add(MediaType.APPLICATION_JSON_VALUE);
        produces.add(MediaType.APPLICATION_XML_VALUE);

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(new ApiInfoBuilder()
                        .title("Note API")
                        .description("A CRUD API to demonstrate Springfox 3 integration")
                        .version("0.0.1")
                        .license("MIT")
                        .licenseUrl("https://opensource.org/licenses/MIT")
                        .build())
                .produces(produces).consumes(produces)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .build();
    }
    
    @Bean
    @Profile("prod")
    public Docket getDocketForProd() {
        final Set<String> produces = new HashSet<String>();
        produces.add(MediaType.APPLICATION_JSON_VALUE);
        produces.add(MediaType.APPLICATION_XML_VALUE);

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(new ApiInfoBuilder()
                        .title("Note API")
                        .description("A CRUD API to demonstrate Springfox 3 integration")
                        .version("0.0.1")
                        .license("MIT")
                        .licenseUrl("https://opensource.org/licenses/MIT")
                        .build())
                .produces(produces).consumes(produces)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .build();
    }

}

现在,在您有多个案卷的每个环境之后,您可以使用此代码行播放:

.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))

并根据每个环境的条件显示端点。

They are multiple way of doing this, one simple approach (but not elegant at all) is to actually create multiple Docket's for different profiles.

Example:

@Configuration
public class SpringFoxConfig {

    @Bean
    @Profile("dev")
    public Docket getDocketForDev() {
        final Set<String> produces = new HashSet<String>();
        produces.add(MediaType.APPLICATION_JSON_VALUE);
        produces.add(MediaType.APPLICATION_XML_VALUE);

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(new ApiInfoBuilder()
                        .title("Note API")
                        .description("A CRUD API to demonstrate Springfox 3 integration")
                        .version("0.0.1")
                        .license("MIT")
                        .licenseUrl("https://opensource.org/licenses/MIT")
                        .build())
                .produces(produces).consumes(produces)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .build();
    }
    
    @Bean
    @Profile("prod")
    public Docket getDocketForProd() {
        final Set<String> produces = new HashSet<String>();
        produces.add(MediaType.APPLICATION_JSON_VALUE);
        produces.add(MediaType.APPLICATION_XML_VALUE);

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(new ApiInfoBuilder()
                        .title("Note API")
                        .description("A CRUD API to demonstrate Springfox 3 integration")
                        .version("0.0.1")
                        .license("MIT")
                        .licenseUrl("https://opensource.org/licenses/MIT")
                        .build())
                .produces(produces).consumes(produces)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .build();
    }

}

Now after you have multiple docket's per environment you can play with this line of code:

.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))

and display endpoints based on your conditions per environment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文