【问题标题】:springfox-data-rest configuration not workspringfox-data-rest 配置不起作用
【发布时间】:2018-05-03 06:12:59
【问题描述】:

我跟着link去配置。

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-data-rest</artifactId>
  <version>2.7.0</version>
</dependency> 

@Configuration
@Import( {springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class})
public class WebMvcConfig extends WebMvcConfigurerAdapter{
     ......
}

@RepositoryRestResource(collectionResourceRel = "mails", path = "mails")
public interface TMessageMailDao extends   PagingAndSortingRepository<TMessageMail, Long>{


}

但是当我打开http://localhost:8080/swagger-ui.html 时,什么都没有。

我知道 springfox-data-rest 仍在孵化中。这就是它不起作用的原因吗?还是有什么问题?

【问题讨论】:

    标签: spring-boot spring-data-rest springfox


    【解决方案1】:
    • 您缺少@EnableSwagger2 注释。
    • 确保创建一个Docket bean,如下例所示。

      @Configuration
      @Import({SpringDataRestConfiguration.class})
      @EnableSwagger2
      public class WebMvcConfig extends WebMvcConfigurerAdapter {
          ...
          @Bean
          public Docket api() {
              return new Docket(DocumentationType.SWAGGER_2)
                  .groupName("example")
                  .select()
                  .paths(PathSelectors.any())
                  .build()
                  .apiInfo(apiInfo("Example API", "Example API"));
          }
      
          private ApiInfo apiInfo(String title, String description) {
               return new ApiInfoBuilder()
                   .title(title)
                   .description(description)
                   .build();
         }
      }
      
    • 确保添加以下依赖项:

      <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger2</artifactId>
          <version>2.7.0</version>
      </dependency>
      <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger-ui</artifactId>
         <version>2.7.0</version>
      </dependency>
      

    一旦它开始工作,您的swagger-ui.html 将如下所示:

    我想扫描两个包裹。如何包含两个基础包而不是一个?

    只是控制器

    如果您只对包含 REST 控制器而不是任何存储库感兴趣,您可以借助自定义方法在 Docket 的 apis 方法中指定任意数量的包。

        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("book")
                    .select()
                    .apis(exactPackages("com.basaki.controller", "com.basaki.model"))
                    .paths(PathSelectors.any())
                    .build()
                    .apiInfo(apiInfo("Example Springfox API",
                            "Example Springfox API"));
        }
    
        private static Predicate<RequestHandler> exactPackages(
                final String... pkgs) {
            return input -> {
                String currentPkg = 
                    input.declaringClass().getPackage().getName();
                for (String pkg : pkgs) {
                    if (pkg.equals(currentPkg)) {
                        return true;
                    }
                }
                return false;
            };
        }
    

    控制器和存储库

    如果您对包含 REST 控制器和存储库感兴趣,则必须借助自定义方法在 Docket 中利用 paths 方法。自定义方法采用路径正则表达式。

        @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("book")
                    .select()
                    .paths(matchPathRegex("/books(/|$).*",
                        "/booxs(/|$).*", "/tokens(/|$).*",
                        "/ping(/|$).*"))
                    .build()
                    .apiInfo(apiInfo("Example Springfox API",
                            "Example Springfox API"));
        }
    
        private static Predicate<String> matchPathRegex(final String... pathRegexs) {
            return new Predicate<String>() {
                @Override
                public boolean apply(String input) {
                    for (String pathRegex : pathRegexs) {
                        if (input.matches(pathRegex)) {
                            return true;
                        }
                    }
                    return false;
                }
            };
        }
    

    【讨论】:

    • 对不起,我没有发布swagger配置,事实上,swagger在我的应用程序中运行良好。我使用spring data rest,我希望spring data rest与swagger集成。springfox只是扫描Controller类, spring data rest使用AbstractRepositoryRestController,它不扩展Controller。
    • 上面的截图是 JPA 存储库的截图,而不是控制器的截图。 Springfox Data REST 为我工作。 Here 是一个完整的工作示例。
    • 非常感谢。你的演示是完美的!我还有一个小问题,要使用 swagger ,我必须注释 .apis(RequestHandlerSelectors.basePackage("com.xxxx")) ,但我想扫描两个包。如何包含两个基础包而不是一个。
    • 用你上次评论的答案更新了我的帖子。
    • 这不适用于 jpa REST 存储库和 spring boot 2
    【解决方案2】:

    感谢 Indra Basak 的帮助。
    我的配置只有一个问题。

    @Configuration
    @EnableSwagger2
    @Import({SpringDataRestConfiguration.class})
    

    这三个注解必须一起使用。我在另一个配置文件中配置@EnableSwagger2。

    【讨论】:

    • 您可以在不同的文件中使用@EnableSwagger2。我通常在我的 swagger 配置文件中声明这个注解
    猜你喜欢
    • 2020-12-16
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    • 2018-07-08
    相关资源
    最近更新 更多