“This is the 20th 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

  • In the last chapter we talked about how to pass, rightApis () interface scanning
  • SpringBoot integration Swagger (3) apis () | Java interface scanning with notes – the nuggets (juejin. Cn)
  • You can also filter interfaces through Paths ()!

The paths () filter

  • First of all, what parameters do I need

    • Click in and see
    •     public ApiSelectorBuilder paths(Predicate<String> selector) {
              this.pathSelector = Predicates.and(this.pathSelector, selector);
              return this;
          }
      Copy the code

(1), PathSelectors. Any ()

  • Same thing. Let’s do an example

    •  @Bean
          public Docket docket(){
              return new Docket(DocumentationType.SWAGGER_2)
                      .apiInfo(apiInfo())
                      .select()
                      .paths(PathSelectors.any())
                      .build();
          }
      Copy the code
    • Let’s restart the project
    • You can see that includeserrorAll of the controllers have been scanned.
  • And then let’s go into PathSelectors and say, what the hell is that?

    • public class PathSelectors { private PathSelectors() { throw new UnsupportedOperationException(); } public static Predicate<String> any() { return Predicates.alwaysTrue(); } public static Predicate<String> none() { return Predicates.alwaysFalse(); } public static Predicate<String> regex(final String pathRegex) { return new Predicate<String>() { public boolean apply(String input) { return input.matches(pathRegex); }}; } public static Predicate<String> ant(final String antPattern) { return new Predicate<String>() { public boolean apply(String input) { AntPathMatcher matcher = new AntPathMatcher(); return matcher.match(antPattern, input); }}; }}Copy the code
    • You can see that there are four arguments: any(), None (), regex(), ant()
    • I want it all. I want it all.
  • Let’s play with each and summarize the differences at the end.

(2), PathSelectors. None ()

  •     @Bean
        public Docket docket(){
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .paths(PathSelectors.none())
                    .build();
        }
    Copy the code
  • Reboot and see what happens
  • Obviously, none of it was shown! I don’t want any of it!

(3), PathSelectors. Regex ()

  • @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .paths(PathSelectors.regex("^[+-@=](.*?) "))// This re matches all.build(); }Copy the code
  • Reboot and see what happens
  • @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .paths(PathSelectors.regex("(/test)([+-@=])(.*?) "))//Java regular expressions are grouped in parentheses to match all controller.build () that begin with /test; }Copy the code
  • Reboot and see what happens
  • Then the result is obvious! Use regular expressions to filter what needs to be shown and what doesn’t!
  • I will write a separate article to play with regular expressions. Here press not table!

(4), PathSelectors. Ant ()

  • @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .paths(PathSelectors. Ant ("/test-swagger2/**"))// Match /test-swagger2/ for all controller.build (); }Copy the code
  • Reboot and see what happens

  • I’m not going to show you too much here, just a little trick.

  • When our project mapping is divided by function:

    • That is, under the test function all scanned!
    • And the user function I do not add, so that will not be scanned!

conclusion

  • Any () // Any requests are scanned
  • None () // No requests are scanned
  • Regex (final String pathRegex) // Control by regular expression
  • Ant (final String antPattern) // Control by ant()
  • Tomorrow will bring Swagger’s switch. How to control multi-environment on/off!
  • The above content is a personal opinion, if there is wrong, please point out!

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