【问题标题】:Automatic Swagger annotation-based description generation in SpringdocSpringdoc中基于Swagger注解的自动描述生成
【发布时间】:2021-04-28 18:36:31
【问题描述】:

基本上我的问题与this one 相同,但针对的是 Springdoc(而不是 Springfox)。

简而言之,我有一个 Spring-boot 应用程序,我正在使用 spring-security @PreAuthorize 注释来保护我的一些 api,目前仅基于 hasAuthority

有没有办法可以根据注解自动修改具体的资源swagger描述?我想这与覆盖 Springdoc 的默认类行为之一有关(可能是 OpenAPICustomiser?),但我不知道该怎么做。

【问题讨论】:

    标签: java swagger swagger-ui springdoc springdoc-openapi-ui


    【解决方案1】:

    好的,我已经用这样的方法解决了-

    @Configuration
    public class SpringdocPreAuthorize {
        @Bean
        public OperationCustomizer operationCustomizer() {
            return (operation, handlerMethod) -> {
                Optional<PreAuthorize> preAuthorizeAnnotation = Optional.ofNullable(handlerMethod.getMethodAnnotation(PreAuthorize.class));
                StringBuilder sb = new StringBuilder();
                if (preAuthorizeAnnotation.isPresent()) {
                    sb.append("This api requires **")
                            .append((preAuthorizeAnnotation.get()).value().replaceAll("hasAuthority|\\(|\\)|\\'", ""))
                            .append("** permission.");
                } else {
                    sb.append("This api is **public**");
                }
                sb.append("<br /><br />");
                sb.append(operation.getDescription());
                operation.setDescription(sb.toString());
                return operation;
            };
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-10-25
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 2017-12-09
      • 2014-08-22
      相关资源
      最近更新 更多