Swagger学习笔记
-
第一步 项目构建
采用传统的springboot项目构建 只需要引入web就行 -
第二步
添加maven依赖
<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>
- 第三步 编写Swagger配置类 一定要去看看源码(小声bb 虽然我也不会看源码)
package com.joisen.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;
import java.util.ArrayList;
/**
* @author : joisen
* @date : 20:09 2020/12/16
*/
@Configuration
@EnableSwagger2 //开启Swagger
public class SwaggerConfig {
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("JOisen1");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("JOisen2");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("JOisen3");
}
//配置了Swagger的Docket的bean实例
@Bean
public Docket docket(Environment environment){
//设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev","test");
//获取项目的环境: ctrl + alt + v
//通过environment.acceptsProfiles判断是否处于自己设定的环境中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)//enable是否启用swagger false表示不能在浏览器中访问swagger
.groupName("Joisen")//配置api文档分组 多个Docket实例即可实现多个分组
.select()
//RequestHandlerSelectors 配置要扫描接口的方式
//basePackage("com.joisen.swagger.controller") 指定要扫描的包
//.any() 全部扫描
//none()都不扫描
//withClassAnnotation(Controller.class) 扫描类上的注解
//withMethodAnnotation(GetMapping.class) 扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.joisen.swagger.controller"))
//.path(PathSelectors.ant("/joisen/**")) joisen包下都不扫描 path指定不扫描路径
//No operations defined in spec! swagger-ui界面没有接口可寻
//.paths(PathSelectors.ant("/joisen/**"))
.build();
}
//配置Swagger信息 = apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact DEFAULT_CONTACT = new Contact("Joisen", "", "1738971281@qq.com");
return new ApiInfo(
"Joisen's Swagger Demo",
"嘿嘿嘿",
"1.0",
"localhost://8080/hello",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
swagger-ui网页上的配置:
- 第四步 编写控制类 与 实体类
@ApiModel("用户实体类") //给生成的文档加注释
public class User {
//private时Swagger扫描不到
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "hello";
}
//只要我们的接口中,返回值中存在实体类,它就会被扫描到Swagger中
@PostMapping("/user")
public User user(){
return new User();
}
@ApiOperation("hello2方法") //给方法加注释
@GetMapping("/hello2") ///ApiParam给参数加注释
public String hello2(@ApiParam("参数用户名") String username){
return "hello" + username;
}
@PostMapping("/postt")
public User postt(User user){
return user;
}
}
试试吧!