大家好 , 我是苏麟 , 今天带来Swagger的使用 .
官方文档 : 招摇文档 (swagger.io)
访问地址 : 在路径后加上doc.html 例如: http://localhost:8000/doc.html
Swagger 使用
依赖
<!--Swagger依赖 核心--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><!--Swagger UI 图形化界面--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><!--加强图形化界面--><dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.6</version></dependency>
配置
server:port: 8000servlet:context-path: /api#如果使用SpringBoot 2.6以上版本,需要在application.properties加入
##解决springboot2.6.2使用swagger的问题
spring:mvc:path match:matching-strategy: ant_path_matcher
在启动类上加入注解
@EnableSwagger2
@EnableSwaggerBootstrapUI
@SpringBootApplication
public class SwaggerApplication {public static void main(String[] args) {SpringApplication.run(SwaggerApplication.class, args);}}
准备一个Swagge配置类
@Configuration
public class SwaggerConfigSetting {@Beanpublic Docket docket() {//创建Docket对象Docket docket = new Docket(DocumentationType.SWAGGER_2);//创建Api信息,接口文档的总体信息ApiInfo apiInfo = new ApiInfoBuilder()//标题.title("苏麟项目")//版本号.version("1.0")//描述.description("前后端分离项目")//联系人.contact(new Contact("苏麟团队", "htttp://sulin.com", "qq.com"))//协议.license("2.0").build();//设置使用docket = docket.apiInfo(apiInfo);//设置文档参与生成的包docket.select().apis(RequestHandlerSelectors.basePackage("com.sl.controller")).build();return docket;}
}
参与文档的类
@Api(tags = "用户功能列表")
@RestController
public class MyController {@ApiOperation(value = "swagger简单使用", notes = "第一个Swagger操作")@GetMapping("/hello")@ApiImplicitParams({@ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String", paramType = "query")})@ApiResponses({@ApiResponse(code = 200,message = "访问成功")})public Student swaggerVoid(String name, Integer age) {Student student = new Student(name, age);return student;}
}
@Data
@AllArgsConstructor
@ApiModel(value = "学生类")
public class Student {@ApiModelProperty(value = "学生姓名")private String name;@ApiModelProperty(value = "学生年龄")private Integer age;
}
knife4j集成版本
访问地址 : 在路径后加上doc.html 例如: http://localhost:8000/doc.html
引入依赖
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.2</version></dependency>
配置
@Slf4j
@Configuration
public class WebSupperConfig extends WebMvcConfigurationSupport {/*** 设置静态资源映射* @param registry*/@Overrideprotected void addResourceHandlers(ResourceHandlerRegistry registry) {log.info("开始进行静态资源映射...");registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}@Beanpublic Docket getDocket() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.sl.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("苏麟").version("1.0").description("苏麟接口文档").build();}}
在启动类上加注解
@EnableSwagger2
@EnableKnife4j
@SpringBootApplication
public class SwaggerApplication {public static void main(String[] args) {SpringApplication.run(SwaggerApplication.class, args);}}
这期就到这里 下期见!
拜拜!