springboot-swagger准备:
springboot2.x
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
Springfox Swagger UI
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
springboot3.0之后只需要导入这个就可以:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
springboot2.x访问: http://localhost:8080/swagger-ui/
springboot3.x访问:
原 swagger-ui.html 已变更,
当前版本url http://localhost:8080/swagger-ui/ 或 http://localhost:8080/swagger-ui/index.html
1.配置Swagger
1、Swagger实例Bean是Docket,所以通过配置Docket实例来配置Swaggger。
@Bean //配置docket以配置Swagger具体参数
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2);
2、可以通过apiInfo()属性配置文档信息(自定义配置)
//配置文档信息
private ApiInfo apiInfo() {
Contact contact = new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱");
return new ApiInfo(
"Swagger学习", // 标题
"学习演示如何配置Swagger", // 描述
"v1.0", // 版本
"http://terms.service.url/组织链接", // 组织链接
contact, // 联系人信息
"Apach 2.0 许可", // 许可
"许可链接", // 许可连接
new ArrayList<>()// 扩展
);
}
3、Docket 实例关联上 apiInfo()
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
4、重启项目,访问测试 http://localhost:8080/swagger-ui/ 或 http://localhost:8080/swagger-ui/index.html**看下效果;
2.配置扫描接口
1、构建Docket时通过select()方法配置扫描接口。
properties配置选择多环境
配置swagger的Docket的bean实例
@Bean
public Docket docket() {
// 设置显示swagger的环境,如果为dev或者test则可以进入网站里
Profiles profile = Profiles.of("dev","test");
// environment.acceptsProfiles判断是否自己处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profile);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(able)//是否启用swagger,为false,则不可以在浏览器中访问swagger
.select()// 通过.select()方法,去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
.apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
.path(PathSelectors.ant("/example/**"))//过滤路径扫描example下的路径
.build();
}
3.分组和接口注释以及小结
@Configuration
@EnableSwagger2 //开启swagger
public class Swaggerconfig {
// 一个docket就是一个实例,对应着一个api接口,必须得自己去配置
@Bean
public Docket docket1()
{
return new Docket(DocumentationType.SWAGGER_2).groupName("分组1");
}
@Bean
public Docket docket2()
{
return new Docket(DocumentationType.SWAGGER_2).groupName("分组2");
}
@Bean
public Docket docket3()
{
return new Docket(DocumentationType.SWAGGER_2).groupName("分组3");
}
//配置了swagger的bean实例
@Bean
public Docket docket (Environment environment)
{
// 设置显示swagger的环境
Profiles profile = Profiles.of("pro","test");
// environment.acceptsProfiles判断是否自己处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profile);
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(flag).select().apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller")).build();
}
private ApiInfo apiInfo()
{
Contact contact = new Contact("lxz","www.http://baidu.com","1250680052@qq.com");
return new ApiInfo("Api Documentation",
"Api Documentation",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
public String name;
@ApiModelProperty("密码")
public Integer password;
}
controller类:
@Api(tags = "hellocontroller")//给类加注解
@RestController
public class TestController {
@ApiOperation("hello控制类")//这个是给方法加注释的
@RequestMapping("/hello")
public String hello(@ApiParam("用户名")String name)//@ApiParam("用户名")给参数加注解
{
return "hello world"+name;
}
//只要接口中返回值中存在实体类,就会被扫描到swagger中
@RequestMapping("/user")
public User user()
{
return new User();
}
}
出于安全考虑:发布时可以关闭swagger,页节省内存