10

I want to display two REST API endpoints in Swagger ui: /cart and /post.

When I specify either /cart or /post works fine but with both showing me error as

No operations defined in spec!

in swagger-ui

@Bean
public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.ant("/cart"))
        .paths(PathSelectors.ant("/post"))
        .build();
}
dbreaux
  • 4,982
  • 1
  • 25
  • 64
Navin Gelot
  • 1,264
  • 3
  • 13
  • 32

6 Answers6

4

Another option is to use .paths(PathSelectors.any()) instead of .paths(PathSelectors.ant("/cart")) and .paths(PathSelectors.ant("/post"))

1

With Spring boot 2.6.x you also need:

spring:  
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
Peyman
  • 250
  • 3
  • 8
  • This trick solves my problem. Can you elaborate how you came to this fix @Peyman ? I'd be great if you can share the original doco/issue point this one out. – tuan.dinh Mar 25 '22 at 01:09
0

It because you use AND condition

  public ApiSelectorBuilder paths(Predicate<String> selector) {
    pathSelector = pathSelector.and(selector);
    return this;
  }

you can combine conditions by using OR clause

.paths(PathSelectors.ant("/cart").or(PathSelectors.ant("/post")))
0

Please add package xxxx as root package so It can scan swagger config in all class

public String getApplicationBasePath() {
                    return swaggerPath;
                }
            }).select().apis(RequestHandlerSelectors.basePackage("xxxx")).build()
                    .securitySchemes(Arrays.asList(securitySchema()))
                    .securityContexts(Collections.singletonList(securityContext())).apiInfo(metaData());
vuhoanghiep1993
  • 715
  • 1
  • 8
  • 15
0

this the solution : is add the the path where exactly you have the controllers classe

exemple : "org.zaid.aitfriha.controller.api" is the path where exactly you have controllers

.apis(RequestHandlerSelectors.basePackage("org.zaid.aitfriha.controller.api"))

dont use org.zaid.* or org.zaid.aitfriha.* or org.zaid.aitfriha.controller.*

Be careful with "/api/AbsenceRequest" dont works it must be "/api/absenceRequest"

@RestController
@RequestMapping("/api/absenceRequest")
@CrossOrigin("*")
Ait Friha Zaid
  • 1,222
  • 1
  • 13
  • 20
0

You can try setting the base package to the RequestHandlerSelectors object in your Swagger Config class. You can point to the specific package where you have your OpenAPI annotations added, i.e. "controller", or just leave it pointing to the root, spring will scan all the nested packages.

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.demo"))
            .paths(PathSelectors.any())
            .build().apiInfo(apiInfoMetaData());
}