1.导入MAVEN
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 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>
2.创建配置类
3.编写配置类
代码如下:
package com.cy.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
# contact包不要导入错误
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;
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
//配置了swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
//contact 是作者信息
Contact contact = new Contact("冷山集", "https://blog.csdn.net/weixin_40597409", "910326532@qq.com");
return new ApiInfo(
"冷山集的Swagger API文档",
"这是一个描述",
"v1.0",
"https://blog.csdn.net/weixin_40597409",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
4.编写Hello类
代码如下:
package com.cy.swagger.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String Hello(){
return "hello";
}
}
5.运行程序检查swagger是否成功启动
输入链接:http://localhost:8080/swagger-ui.html
6.Swagger配置扫描接口
更新配置类代码如下:
package com.cy.swagger.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
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;
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
//配置了swagger的Docket的bean实例
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// RequestHandlerSelectors配置要扫描的接口方式
// .basePackage指定要扫描的包
// .any()全部扫描 .none()全部不扫描
// withClassAnnotation() 扫描类上的注解,参数是一个注解的反射对象(.class)
// withMethodAnnotation() 扫描类上的注解
.apis(RequestHandlerSelectors.basePackage("com.cy.swagger.controller"))
// .paths() 过滤
// .paths(PathSelectors.ant("/cy/**"))
.build();
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
//contact 是作者信息
Contact contact = new Contact("冷山集", "https://blog.csdn.net/weixin_40597409", "910326532@qq.com");
return new ApiInfo(
"冷山集的Swagger API文档",
"这是一个描述",
"v1.0",
"https://blog.csdn.net/weixin_40597409",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
配置是否启动了Swagger
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// .enable(false) 是否启动Swagger
.enable(false)
.select()
// RequestHandlerSelectors配置要扫描的接口方式
// .basePackage指定要扫描的包
// .any()全部扫描 .none()全部不扫描
// withClassAnnotation() 扫描类上的注解,参数是一个注解的反射对象(.class)
// withMethodAnnotation() 扫描类上的注解
.apis(RequestHandlerSelectors.basePackage("com.cy.swagger.controller"))
// .paths() 过滤
// .paths(PathSelectors.ant("/cy/**"))
.build();
}
扩展题目:
问:只希望在生产时候使用swagger,发布的时候不使用,如何做?
思路:
1.判断是否为生产环境 flag=false
2.注入enable()参数
1.创建两个新的配置文件
2.进行文件配置
此处配置是选择激活哪个环境(此处为dev下的8081端口)
3.设置配置类
代码如下:
package com.cy.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.PathSelectors;
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;
@Configuration
@EnableSwagger2 //开启swagger2
public class SwaggerConfig {
//配置了swagger的Docket的bean实例
@Bean
public Docket docket(Environment environment){
//设置要显示的swagger环境
Profiles profiles = Profiles.of("dev","test");
//获取项目的环境:
//通过environment.acceptsProfiles()判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(flag)
.select()
.apis(RequestHandlerSelectors.basePackage("com.cy.swagger.controller"))
.build();
}
//配置swagger信息=apiInfo
private ApiInfo apiInfo(){
//contact 是作者信息
Contact contact = new Contact("冷山集", "https://blog.csdn.net/weixin_40597409", "910326532@qq.com");
return new ApiInfo(
"冷山集的Swagger API文档",
"这是一个描述",
"v1.0",
"https://blog.csdn.net/weixin_40597409",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
}
7.配置分组
在配置文件下加一行代码:
.groupName("冷山集")
重启服务器登入界面可看到:
所以发现Docket对象对应一个分组,则可以在SwaggerConfig配置类中加入如下代码:
@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");
}
重启服务器登入页面可发现:
8.实体类配置
新建pojo包下的User对象:
代码如下
package com.cy.swagger.pojo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
private String username;
@ApiModelProperty("密码")
private String password;
}
然后编辑HelloController,代码如下:
package com.cy.swagger.controller;
import com.cy.swagger.pojo.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String Hello(){
return "hello";
}
//只要接口中.返回值存在实体类,它就会被扫描到Swagger中
@PostMapping("/user")
public User user(){
return new User();
}
}
重启服务器:
其他注释格式化参数:
@ApiParam(“用户名”)
@ApiOperation(“hello控制类的user”)
@ApiOperation("hello控制类的user")
@PostMapping("/user2")
public User user2(@ApiParam("用户名") String username){
return new User();
}
9.总结
- 可以通过Swagger给一些比较难理解的接口或者属性增加注释信息
- 接口文档实时更新
- 可以在线测试
注:
正式发布的时候关闭Swagger.(出于安全考虑,且节省运行内存)