Swagger integration support for servlet in springboot project

46 views
Skip to first unread message

Rijuta

unread,
Nov 20, 2018, 9:57:25 AM11/20/18
to Swagger
Hi, 
We have a Java-Servlet project that we recently migrated to Springboot. However, some of the endpoints are still Servlet endpoints. 
I am trying to add Swagger documentation but the swagger doesn't detect the Servlets as endpoint. I tired a few online solutions, no success yet. 
Is it possible to document Servlet with Swagger? Any example to refer? Thank you. 

Below are current implementation details  - 

Servlet class look like: 

public class StatusServlet extends HttpServlet {
   
    /**
     * Initialize the servlet.
     */
    public void init() {
        String myName = getServletName();
        ApplicationController.setProperty(
            Constants.STATUS_SERVLET_NAME, myName);
    }

    /**
     * doGet method.
     * @param request the request object
     * @param response the response object
     * @throws ServletException if a ServletException is generated
     * @throws IOException if an IOException is generated
     */
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

       //Implementation goes here
    }
}

Swagger config: 

@Configuration
@EnableSwagger2
public class SwaggerConfig {           
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)
          .select()                                 
         .apis(RequestHandlerSelectors.basePackage("<package name>"))
          .paths(PathSelectors.any()).paths(paths())
          .build().apiInfo(apiInfo());                                           
    }
    
 // Describe your apis
  private ApiInfo apiInfo() {
  //String appName = appProperties.getApplcationName();
  String appName = "xyz";
  return new ApiInfoBuilder().title(appName + " APIs")
  .description("This page lists all the APIs for the " + appName)
  //.version(appProperties.getVersion())
  .license("Apache 2.0")
  .build();
  }

  // Only select apis that matches the given Predicates.
  private Predicate<String> paths() {
 
  // Match all paths except /error
  return Predicates.and(PathSelectors.regex("/.*"), Predicates.not(PathSelectors.regex("/error.*")));
  }


  @Bean
  UiConfiguration uiConfig() {
  return UiConfigurationBuilder.builder()
  .deepLinking(true)
  .displayOperationId(false)
  .defaultModelsExpandDepth(1)
  .defaultModelExpandDepth(1)
  .defaultModelRendering(ModelRendering.EXAMPLE)
  .displayRequestDuration(false)
  .docExpansion(DocExpansion.NONE)
  .filter(false)
  .maxDisplayedTags(null)
  .operationsSorter(OperationsSorter.ALPHA)
  .showExtensions(false)
  .tagsSorter(TagsSorter.ALPHA)
  .validatorUrl(null)
  .build();
  }
}

Gradle dependency: 
//Springfox for Swagger Support
//compile('io.springfox:springfox-swagger2:2.8.0')
//compile('io.springfox:springfox-swagger-ui:2.8.0')
Reply all
Reply to author
Forward
0 new messages