Swagger + knife 配置
在学习Mall项目中,用到了Swagger2对项目接口进行页面展示及测试,Swagger3可以进行插件增强,所以进行了升级
Swagger3
配置
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> <exclusions> <exclusion> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.21</version> </dependency>
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
|
配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| package com.yancy0109.mall.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; 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.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket;
@EnableKnife4j @Configuration @EnableOpenApi public class Swagger3Config {
@Bean public Docket docket(Environment environment){ Profiles profiles = Profiles.of("dev","test"); boolean flag = environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(flag)
.groupName("Mall电商") .select()
.apis(RequestHandlerSelectors.basePackage("com.yancy0109.mall.controller")) .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any()) .build(); }
private ApiInfo apiInfo(){ Contact contact = new Contact("yancy0109", "https://yancy0109.github.io/", "1156729237@qq.com"); return new ApiInfoBuilder() .title("Swagger-UI") .description("Mall-Learing") .contact(contact) .version("1.0") .build(); } }
|
配置类说明
对启用模式设置补充说明
我们的项目中并不会在上线项目继续开启Swagger,而在Swagger Docket注入时,可以配置是否启用
1 2 3 4
| Profiles profiles = Profiles.of("dev","test");
boolean flag = environment.acceptsProfiles(profiles);
|
flag值及项目运行环境是否为Swagger环境
环境设置
这部分是对yaml配置进行补充,application.yaml可以开启指定配置,格式为application-xxx.yaml,引用时以逗号分隔
application.yaml
1 2 3 4
| spring: profiles: active: dev
|
application-dev.yaml
application-pro.yaml
使用
注解
接口注解
@Api()
@ApiOperation()
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
@Api(tags = "PmsBrandController") @RestController @RequestMapping("/brand") public class PmsBrandController {
@Autowired PmsBrandService pmsBrandService;
private static final Logger LOGGER = LoggerFactory.getLogger(PmsBrandController.class);
@ApiOperation("获取所有品牌类别") @GetMapping("/listAll") public CommonResult listAll(){ return CommonResult.success(pmsBrandService.listAllBrand()); }
@ApiOperation("添加品牌") @PostMapping("/create") public CommonResult createBrand(PmsBrand pmsBrand){ boolean flag = pmsBrandService.createBrand(pmsBrand); if (flag){ return CommonResult.success(); }else { LOGGER.info("createBrand操作失败"); return CommonResult.failed(); } } }
|
实体类注解
@ApiModel
@ApiModelProperty
实例
Swagger2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| package com.yancy0109.mall.mbg.model;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable;
@ApiModel public class PmsBrand implements Serializable { private Long id;
private String name;
@ApiModelProperty(value = "首字母") private String firstLetter;
private Integer sort;
@ApiModelProperty(value = "是否为品牌制造商:0->不是;1->是") private Integer factoryStatus;
private Integer showStatus;
@ApiModelProperty(value = "产品数量") private Integer productCount;
@ApiModelProperty(value = "产品评论数量") private Integer productCommentCount;
@ApiModelProperty(value = "品牌logo") private String logo;
@ApiModelProperty(value = "专区大图") private String bigPic;
@ApiModelProperty(value = "品牌故事") private String brandStory;
private static final long serialVersionUID = 1L; 方法省略....... }
|
Swagger3
略有区别
1
| @ApiModelProperty(value ="邮费", required =true, example ="0.00")
|
访问Url
Swagger2
/swagger-ui.html
Swagger3
/swagger-ui/index.html
Swagger3增强
/doc.html