整了一个根据当前运行环境是否为 (dev,test,pro) 去显示是否开启swagger:
@Bean
public Docket docket(Environment environment) { // 设置要显示的swagger 环境 Profiles p = Profiles.of("dev","test"); // 通过environment.acceptsProfiles 判断是否处在自己设定的环境中 boolean b = environment.acceptsProfiles(p); return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .enable(b) .select() .apis(RequestHandlerSelectors.basePackage("top.nosoul.controller")) .build(); }
运行项目之后 发现显示:
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
解决办法:
将端口号改成 设置环境对应的端口号