目录
swagger入门
导入依赖
配置swagger--->Config
测试:http://localhost:8080/swagger-ui.html
配置Swagger
源码:
代码:
配置扫描接口
接口过滤
配置是都启动Swagger
问题: 希望Swagger 在生产环境中使用,在发布的时候不使用?
配置Api文档分组
问题:如何配置多个组 多个Docket实例即可
实体类配置
user
HelloController
目录:
SwaggerConfig
HelloController
User
总结:
swagger入门
导入依赖
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
配置swagger--->Config
@Configuration
@EnableSwagger2 //开启Swagger2的自动装配
public class SwaggerConfig {
}
测试:http://localhost:8080/swagger-ui.html
io.springfox
springfox-swagger2
2.9.2
io.springfox
springfox-swagger-ui
2.9.2
配置swagger--->Config
@Configuration
@EnableSwagger2 //开启Swagger2的自动装配
public class SwaggerConfig {
}
测试:http://localhost:8080/swagger-ui.html
配置Swagger
Swagger的bean实例Docket
源码:
@Bean
public Docket docket1() {
return new Docket(documentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
//配置Swagger2的apiInfo信息
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact("gh", "https://blog.csdn.net/gh_xiaohe?spm=1000.2115.3001.5343", "2495140780@qq.com");
return new ApiInfo(
"Swagger2 API文档", //标题
"文档描述写啥我也不知道!!!", //描述
"V1.0", //版本信息
"localhost:8080/hello", //组织链接
contact, //联系人信息
"Apache 2.0", //许可
"http://www.apache.org/licenses/LICENSE-2.0", //许可连接
new ArrayList() //扩展
);
}
代码:
@Bean
public Docket docket1() {
return new Docket(documentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
@Bean
public Docket docket1() {
return new Docket(documentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
//配置Swagger2的apiInfo信息
private ApiInfo apiInfo() {
//作者信息
Contact contact = new Contact(
"gh",
"https://blog.csdn.net/gh_xiaohe",
"2495140780@qq.com");
return new ApiInfo(
"Swagger2 API文档", //标题
"文档描述写啥我也不知道!!!", //描述
"V1.0", //版本信息
"https://www.bilibili.com/", //组织链接
contact, //联系人信息
"Apache 2.0", //许可
"http://www.apache.org/licenses/LICENSE-2.0", //许可连接
new ArrayList() //扩展
);
}
配置扫描接口
接口过滤
Docket.select()
Docket.select()
@Bean
public Docket docket1() {
return new Docket(documentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.build();
}
@Bean
public Docket docket() {
return new Docket(documentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
.build();
}
@Bean
public Docket docket() {
return new Docket(documentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
//过滤 过滤什么路径
.paths(PathSelectors.ant("/gh
.apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
//过滤 过滤什么路径
// .paths(PathSelectors.ant("/gh
.apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
//过滤 过滤什么路径
// .paths(PathSelectors.ant("/gh
@Bean
public Docket docket1(){
return new Docket(documentationType.SWAGGER_2).groupName("AAA");
}
@Bean
public Docket docket2(){
return new Docket(documentationType.SWAGGER_2).groupName("BBB");
}
@Bean
public Docket docket3(){
return new Docket(documentationType.SWAGGER_2).groupName("CCC");
}
实体类配置
@ApiModel
@ApiModelProperty
@Api(注释)
@ApiOperation("Hello控制类") //Operation 接口 不是放在类上,是方法
@ApiParam 方法形参
user
//给生成的文档加一个注释
@Api(注释)
@ApiModel("User:用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
HelloController
@ApiModel
@ApiModelProperty
@Api(注释)
@ApiOperation("Hello控制类") //Operation 接口 不是放在类上,是方法
@ApiParam 方法形参
//给生成的文档加一个注释
@Api(注释)
@ApiModel("User:用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
HelloController
只和 返回值 中 有无实体类有关 和有无注解无关
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "Hello,Swagger";
}
//只要我们的接口中,返回值中存在实体类,他就会被扫描到 Swagger 中
@GetMapping("/user")
public User user1(){
return new User();
}
}
//Operation 接口 不是放在类上,是方法
@ApiOperation("Hello控制类")
@PostMapping("/hello2")
public String hello2(@ApiParam("用户名") String name){
return name;
}
目录:
SwaggerConfig
package com.gh.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 org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
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 {
@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())
.groupName("Hello-Swagger")
//enable 是否启动 Swagger 如果为false 则 Swagger 不能在浏览器访问
.enable(flag) //默认true
.select()
.apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
//过滤 过滤什么路径
// .paths(PathSelectors.ant("/gh
@Bean
public Docket docket1(){
return new Docket(documentationType.SWAGGER_2).groupName("AAA");
}
@Bean
public Docket docket2(){
return new Docket(documentationType.SWAGGER_2).groupName("BBB");
}
@Bean
public Docket docket3(){
return new Docket(documentationType.SWAGGER_2).groupName("CCC");
}
}
HelloController
package com.gh.controller;
import com.gh.pojo.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "Hello,Swagger";
}
//只要我们的接口中,返回值中存在实体类,他就会被扫描到 Swagger 中
@GetMapping("/user")
public User user(){
return new User();
}
//Operation 接口 不是放在类上,是方法
@ApiOperation("Hello控制类")
@GetMapping("/hello2")
public String hello2(@ApiParam("用户名") String name){
return name;
}
@ApiOperation("post")
@PostMapping("/post")
public User post(@ApiParam("用户名") User user){
return user;
}
}
User
//给生成的文档加一个注释
//@Api(注释)
@ApiModel("User:用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
总结:
我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息接口文档实时更新可以在线测试
package com.gh.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 org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
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 {
@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())
.groupName("Hello-Swagger")
//enable 是否启动 Swagger 如果为false 则 Swagger 不能在浏览器访问
.enable(flag) //默认true
.select()
.apis(RequestHandlerSelectors.basePackage("com.gh.controller"))
//过滤 过滤什么路径
// .paths(PathSelectors.ant("/gh
@Bean
public Docket docket1(){
return new Docket(documentationType.SWAGGER_2).groupName("AAA");
}
@Bean
public Docket docket2(){
return new Docket(documentationType.SWAGGER_2).groupName("BBB");
}
@Bean
public Docket docket3(){
return new Docket(documentationType.SWAGGER_2).groupName("CCC");
}
}
HelloController
package com.gh.controller;
import com.gh.pojo.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(){
return "Hello,Swagger";
}
//只要我们的接口中,返回值中存在实体类,他就会被扫描到 Swagger 中
@GetMapping("/user")
public User user(){
return new User();
}
//Operation 接口 不是放在类上,是方法
@ApiOperation("Hello控制类")
@GetMapping("/hello2")
public String hello2(@ApiParam("用户名") String name){
return name;
}
@ApiOperation("post")
@PostMapping("/post")
public User post(@ApiParam("用户名") User user){
return user;
}
}
User
//给生成的文档加一个注释
//@Api(注释)
@ApiModel("User:用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
总结:
我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息接口文档实时更新可以在线测试
//给生成的文档加一个注释
//@Api(注释)
@ApiModel("User:用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
总结:
我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息接口文档实时更新可以在线测试
【注意点】在正式发布的时候,关闭Swagger!!!出于安全考虑。而且节省运行的内存;



