Hide endpoint on swagger 3.0 UI

Viewed 2952

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?

2 Answers
  • Try adding @ApiIgnore to your method in controller class
  • Another way is to add @ApiOperation(hidden = true)

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();
    }
}
Related