“This is the 9th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”


Related articles

Java with Notes: Java with Notes


preface

  • Dear friends, we have been talking about the configuration of document information. Have you ever wondered why our Swagger is connected abovebasic-error-controllerWill be scanned?
  • Let’s take a look at why

Select () configures the scanning interface

  • As I said, a Docket is a chain call.

  • To use select(), you must build()

  • Let me give you an example of what’s inside.

    •     @Bean
          public Docket docket(){
              return new Docket(DocumentationType.SWAGGER_2)
                      .apiInfo(apiInfo())
                      .select()
                      .apis(RequestHandlerSelectors.none())
                      .build();
          }
      Copy the code
  • Start and take a look

    • Ah! There’s nothing left.
  • What is the parameter RequestHandlerSelectors in apis()?

    • Key methods have been framed.

      • When we set to RequestHandlerSelectors. None (), not scan any interface.

      • When we set to RequestHandlerSelectors. Any (), scan all interfaces.

      • When we set to RequestHandlerSelectors. BasePackage (” need to scan the package path “)

        •     @Bean
              public Docket docket(){
                  return new Docket(DocumentationType.SWAGGER_2)
                          .apiInfo(apiInfo())
                          .select()
                          .apis(RequestHandlerSelectors.basePackage("com.dayu.dyswagger.dycontroller"))
                          .build();
              }
          Copy the code
      • This allows us to specify the scan, which interfaces need to be shown and which interfaces need not be shown.

      • What if I need to match controllers with multiple packages?

        • Or usepathCome, or usewithClassAnnotation. I’ll talk about path next. So how do we use withClassAnnotation
        • Obviously, this is the designationannotations.
        • So we can imagine: set all plus@RestControllerAnnotated classes can be scanned, right? Or we can write a note and add it to the Controller we need to scan, right? The following shows a wave of ~
      • Scan specified annotations:

        •     @Bean
              public Docket docket(){
                  return new Docket(DocumentationType.SWAGGER_2)
                          .apiInfo(apiInfo())
                          .select()
                          .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                          .build();
              }
          Copy the code
        • Success!!!!! But it’s not very flexible, right? Because these native annotations are the ones we use all the time, it’s impossible to avoid a class not adding them.
      • Custom annotations

        • /** * @program: dyswagger * @description: Swagger scan annotation * @author: DingYongJun * @create: 2021-11-10 22:03 */ @Retention(value = RetentionPolicy.RUNTIME) @Target(value = ElementType.TYPE) @Inherited public @interface StartSwaggerScan { }Copy the code
        • Add @startSwaggerScan to the classes that need to be scanned

        •     @Bean
              public Docket docket(){
                  return new Docket(DocumentationType.SWAGGER_2)
                          .apiInfo(apiInfo())
                          .select()
                          .apis(RequestHandlerSelectors.withClassAnnotation(StartSwaggerScan.class))
                          .build();
              }
          Copy the code
        • Run and see results

        • Perfect to meet the requirements!

      • This approach is relatively flexible! Play it any way you want! Ha ha ha!

    • To sum up:

      • Any () // Scan all. All interfaces in the project will be scanned
      • None () // Does not scan interfaces
      • Scanning with annotations on methods, such as withMethodAnnotation(getMapping.class) scans only get requests
      • Scanning with annotations on a class, such as.withClassAnnotation(Controller.class) scans only interfaces in classes with Controller annotations
      • BasePackage (Final String basePackage) // Scans the interface based on the packet path

The road ahead is long, I see no end, I will search high and low

If you think I bloggers write well! Writing is not easy, please like, follow, comment and give encouragement to the blogger ~ Hahah