maven
1 2 3 4 5
   | <dependency>     <groupId>io.springfox</groupId>     <artifactId>springfox-boot-starter</artifactId>     <version>3.0.0</version> </dependency>
   | 
 
 Swagger3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   | @EnableOpenApi @Configuration public class Swagger3Config {
      
 
 
 
      @Value("${swagger.enabled}")     private boolean enable;
      @Bean     public Docket createRestApi() {         return new Docket(DocumentationType.OAS_30)                 .enable(enable)                 .apiInfo(apiInfo())                 .select()                 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))                 .paths(PathSelectors.any())                 .build();     }
      private ApiInfo apiInfo() {         return new ApiInfoBuilder()                 .title("流程服务")                 .description("流程服务接口")                 .version("0.0.1")                 .build();     } }
   | 
 
其他注解和swagger2一样
 knife4j
由于swagger3不支持导出,这里引入knife4j
1 2 3 4 5
   | <dependency>     <groupId>com.github.xiaoymin</groupId>     <artifactId>knife4j-spring-boot-starter</artifactId>     <version>3.0.1</version> </dependency>
   | 
 
然后在swagger配置文件添加注解
1 2 3 4
   | @EnableKnife4j @Configuration public class Swagger3Config { }
   | 
 
 生产环境
生产环境需要关闭swagger~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   | @Slf4j @SpringBootApplication @PropertySource({"file:${IDC_HOME}/conf/conf.properties"}) public class TestApplication {
      private static final String line = "\n----------------------------------------------------------";
      public static void main(String[] args) throws UnknownHostException{         ConfigurableApplicationContext context = SpringApplication.run(TestApplication.class, args);         String swaggerEnabled = context.getEnvironment().getProperty("swagger.enabled");         afterRun(context, Boolean.parseBoolean(swaggerEnabled));     }
      private static void afterRun(ConfigurableApplicationContext applicationContext, boolean swaggerEnabled) throws UnknownHostException {         if (swaggerEnabled) {             AbstractServletWebServerFactory webServerFactory = applicationContext.getBean(AbstractServletWebServerFactory.class);             int port = webServerFactory.getPort();             String contextPath = webServerFactory.getContextPath();
              log.info("\n访问swaggger接口调试 URLs:" +                             line +                             "\n" + "Local: \t\thttp://{}:{}{}/swagger-ui/index.html" +                             line                     , InetAddress.getLocalHost().getHostAddress(), port, contextPath);
              log.info("\n访问knife4j接口调试 URLs:" +                             line +                             "\n" + "Local: \t\thttp://{}:{}{}/doc.html" +                             line                     , InetAddress.getLocalHost().getHostAddress(), port, contextPath);         }     } }
   | 
 
 最后
本文到此结束,感谢阅读。如果您觉得不错,请关注公众号【当我遇上你】,您的支持是我写作的最大动力。