No operations defined in spec! while specifying multiple paths in swagger ui and springfox
Asked Answered
C

6

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();
}
Chamkis answered 11/5, 2018 at 11:26 Comment(4)
Is this Springfox or some other framework?Photostat
no it is springfoxChamkis
return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage(("com.valens.compony"))) .paths(Predicates.or( PathSelectors.regex("/cart"),PathSelectors.regex("/post"))) .build() .pathMapping("/") i found this solution but it only works in java not in scalaChamkis
Verify the endpoint root path.Stepdame
C
4

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

Claro answered 31/12, 2020 at 13:37 Comment(0)
M
1

With Spring boot 2.6.x you also need:

spring:  
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
Muskogean answered 9/2, 2022 at 15:19 Comment(0)
G
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")))
Giro answered 14/2, 2022 at 16:29 Comment(0)
J
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());
Jablon answered 7/12, 2022 at 7:31 Comment(0)
B
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("*")
Brendanbrenden answered 31/5, 2023 at 14:16 Comment(0)
A
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());
}
Appetite answered 27/7, 2023 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.