Spring Boot配置Springdoc

刚刚开通了一个公众号,会分享一些技术博客和自己觉得比较好的项目,同时会更新一些自己使用的工具和图书资料,后面会整理一些面试资料进行分享,觉得有兴趣的可以关注一下。
在这里插入图片描述

问题描述

之前文章有提到Spring Boot切换到Springdoc,使用的是注解配置。但是每个API是有规范的,最近检查的时候,需要统一定义异常返回。这个时候注解就显得没那么有用了。需要改为编程式的,统一配置。


解决方案:

直接上代码:

@Configuration
public class SpringDocConfiguration {

    @Value("${spring.profiles.active:prod}")
    private String profile;
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("PROJECT - " + profile)
                        .version("1.0-SNAPSHOT")
                        .description("API Resources & Documentation")
                        .contact(new Contact().name("leeffy").email("leeffy@qq.com"))
                )
                .addSecurityItem(new SecurityRequirement().addList("NewRiskAuthorize"))
                .components(new Components()
                        .addSecuritySchemes("NewRiskAuthorize",
                                new SecurityScheme()
                                        .description("JWT认证")
                                        .in(SecurityScheme.In.HEADER)
                                        .type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT")));
    }
//    @Bean
//    public OpenApiCustomiser globalResponseStatusCustomizer() {
//        return openApi -> {
//            openApi.getComponents().getSchemas().forEach((key, schema) -> schema.additionalProperties(false));
//            openApi.getPaths().values().forEach(pathItem -> pathItem.readOperations().forEach(operation -> {
//                ApiResponses apiResponses = operation.getResponses();
//                apiResponses.addApiResponse("401", createApiResponse("Unauthorized"));
//                apiResponses.addApiResponse("403", createApiResponse("Unauthorized"));
//                apiResponses.addApiResponse("406", createApiResponse("Could not find acceptable representation"));
//                apiResponses.addApiResponse("429", createApiResponse("Request too much"));
//            }));
//        };
//    }

//    private ApiResponse createApiResponse(String message) {
//        return new ApiResponse().description(message).content(new Content().addMediaType("text/plain",
//                new MediaType().schema(new Schema().$schema("string").pattern(".*").minLength(1).maxLength(50))));
//    }
}

注释的部分是统一定义返回响应码的地方。

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻D开始

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值