swagger 3.0 版本在java 工程中的使用时踩了不少坑, 起初配置好后,页面打不开,或打开时报错。现记录一下。
在pom.xml 中引入依赖。必须引入 swagger2,不然页面打不开:
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
application.yml 文本中添加配置:
springfox:
documentation:
swagger-ui:
enabled: true
添加 一个配置文件类 Swagger2Config.java
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @description: SwaggerConfig * @author: willhu * @date: 2025/4/2 9:25 */ @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket webApiConfig() { ApiInfo apiInfo = new ApiInfoBuilder() .title("ComSight报表API") .description("ComSight报表API接口文档") .version("1.0") .contact(new Contact("willhu", "https://gitee.com/tototsu", "sunnydayhu@163.com")) .license("我的首页") .licenseUrl("https://gitee.com/tototsu") .build(); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo) .select() .apis(RequestHandlerSelectors.basePackage("com.comfort.api")) .paths(PathSelectors.any()) .build(); } }
在 spring-boot 启动类上添加注解:
@EnableSwagger2
本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处。
浙公网安备 33010602011771号