2

I want to have one private endpoint, which should be hidden on Swagger API UI.

referring to some other posts I created Filter class as below.

public class Myfilter implements SwaggerSpecFilter
public class MySwaggerSpecFilter implements SwaggerSpecFilter {
  @Override
     public boolean isOperationAllowed(Operation operation, ApiDescription api, Map<String, List<String>> params,
             Map<String, String> cookies, Map<String, List<String>> headers){}
 
 @Override
     public boolean isParamAllowed..
 
 @Override
     public boolean isPropertyAllowed...
 }

public MyApplication extends Application {
@Override
 public Set<Class<?>> getClasses()
 {
     final Set<Class<?>> classes = new HashSet<>();

     // Set Swagger Filter
     FilterFactory.setFilter(new MySwaggerSpecFilter());
}
}

Web.xml

<servlet>
     <servlet-name>myservlet</servlet-name>
     <servlet-class>ServletClass</servlet-class>
     <init-param>
         <param-name>javax.ws.rs.Application</param-name>
         <param-value>PATHTO/MyApplication</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
 </servlet>

I see that isOperationAllowed is not getting called while loading Swagger UI. When would the method isOperationAllowed get called?

Bosco
  • 3,835
  • 6
  • 25
  • 33

2 Answers2

1
  • Try adding @ApiIgnore to your method in controller class
  • Another way is to add @ApiOperation(hidden = true)
vijayan007
  • 366
  • 5
  • 13
  • 1
    I cannot update controller class, Swagger auto generates it. I can update *.yaml file, but couldn't find any setting that would hide the API (create above annotations in controller) – Bosco Jul 06 '20 at 17:31
  • Oh, you use contract first model? You create .yml first and from there you generate the java source code? – vijayan007 Jul 07 '20 at 18:27
0

As you already specified you can't use any annotation on top of the controller because the endpoint you want to hide is auto-generated. In this case one of the possible solutions could be instead for selecting paths by any() selector use ant() selector as given below-

Instead of using this .paths(PathSelectors.any()) use .paths(PathSelectors.ant("/path-to-match/**"))

Being specific to spring-boot instead of using below code -

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())     <<<<<
            .build();
    }
}

use below code -

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.ant("/path-to-match/**")) <<<<<
            .build();
    }
}
Vipul Kumar
  • 423
  • 4
  • 11