赞
踩
Swagger在前后端交互的时候,提供给前端人员看的文档,具体流程如下:
第一步,导入依赖:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
第二步,书写配置类(以后用直接拷贝):
@Configuration @EnableSwagger2 // 开启swagger文档 public class SwaggerConfig { @Bean public Docket docket() { ApiInfo apiInfo = new ApiInfoBuilder() .title("XXX系统接口文档") .version("v1.0") .description("XXXXXXXXXXXXXXXXXXXXX") .contact(new Contact("张三", "http://localhost:8080/", "222@qq.com")) .build(); Docket docket = new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo) .select() // 开始选择公布那些接口 .apis(RequestHandlerSelectors.basePackage("org.example.controller")) .paths(PathSelectors.any()) .build() .ignoredParameterTypes(HttpSession.class, MultipartFile.class, HttpServletRequest.class); // 忽略掉 HttpSession, MultipartFile, HttpServletRequest. return docket; } }
第三步,启动服务器,在浏览器输入:
http://localhost:port/swagger-ui.html
swagger生成的文档不好看,然后就有了knife4j,他是在swagger的基础上又进行封装,说白了就是加了一层皮。
第一步,导入依赖:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<!--在引用时请在maven中央仓库搜索2.X最新版本号-->
<version>2.0.8</version>
</dependency>
第二步,在swagger的配置类上加上
@EnableKnife4j
注解即可
@Configuration @EnableKnife4j @EnableSwagger2 // 开启swagger文档 public class SwaggerConfig { @Bean public Docket docket() { ApiInfo apiInfo = new ApiInfoBuilder() .title("XXX系统接口文档") .version("v1.0") .description("XXXXXXXXXXXXXXXXXXXXX") .contact(new Contact("张三", "http://localhost:8080/", "222@qq.com")) .build(); Docket docket = new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo) .select() // 开始选择公布那些接口 .apis(RequestHandlerSelectors.basePackage("org.example.controller")) .paths(PathSelectors.any()) .build() .ignoredParameterTypes(HttpSession.class, MultipartFile.class, HttpServletRequest.class); // 忽略掉 HttpSession, MultipartFile, HttpServletRequest. return docket; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。