Search code examples
javaspring-bootswaggerswagger-uiswagger-2.0

How to hide only one mapping from swagger ui documentation if I more than one mapping for method in Spring boot


I've one spring boot rest controller method which is mapped to multiple mappings. Please find the example code below.

@RestController
public class HomeController {

    @RequestMapping( {"/", "/home"} )
    public String home() {
        return "Hello, World!";
    }

}

I want to hide /home mapping from swagger documentation.

Can someone please help me to achieve this.


Solution

  • I also searched for a way to hide certain URLs from multi mapping methods. Unfortunately, I don't think it's possible when multi mapping it's defined like this @RequestMapping( {url1, url2} )

    There are 2 alternative ways to do it:

    1. Split your method into 2 methods that call the same function and annotate the one you want to hide with @Operation(hidden=true)

    2. Define exceptions in your swagger config (for swagger 3 which uses open API):

       @Configuration
       public class SwaggerConfig {
      
           @Bean
           public GroupedOpenApi myApi()
           {
               return GroupedOpenApi.builder()
                   .pathsToMatch("/**")
                   .pathsToExclude("/home")
                   .build();
           }
       }