目录
1. pom.xml文件中添加Swagger的jar包
2. 配置Swagger
3. 项目启动中加入Swagger注解的开关,启动Swagger功能
4. 启动项目,查看效果
Swagger 的功能这里就不多说明了,相信大家都懂的,好奇多问一句,大家有知道其他类似Swagger的替代品吗?欢迎留言一起交流!!
只需要三步,快速启用Swagger功能,让你的项目实现Swagger在线文档,实时浏览,修改展示
1. pom.xml文件中添加Swagger的jar包
2. 配置Swagger
3. 项目启动中加入Swagger注解的开关,启动Swagger功能
具体如下:
1. pom.xml文件中添加Swagger的jar包
我这里使用的spring boot是2.1.4的版本
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version></parent>
Swagger 依赖:
<!-- swagger --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.1</version></dependency><!-- swagger-ui --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.10.0</version></dependency><!-- 解决 Illegal DefaultValue null for parameter type integer 异常 --><dependency><groupId>io.swagger</groupId><artifactId>swagger-annotations</artifactId><version>1.5.21</version></dependency><dependency><groupId>io.swagger</groupId><artifactId>swagger-models</artifactId><version>1.5.21</version></dependency>
2. 配置Swagger
@Configuration
@EnableSwagger2
public class Swagger2 {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.gcc.account")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("GCC Account API").contact(new Contact("gcc", "", "")).version("1.0").description("API描述").build();}
}
3. 项目启动中加入Swagger注解的开关,启动Swagger功能
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class
})
@EnableSwagger2
public class AccountApplication {public static void main(String[] args) {SpringApplication.run(AccountApplication.class, args);}}
4. 启动项目,查看效果
访问地址: http://localhost:8011/gccaccount/swagger-ui.html#/