在springboot中配置knife4j
-
Swagger介绍
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务
官网:https://swagger.io/autolink。
它的主要作用是:
- 使得前后端分离开发更加方便,有利于团队协作
- 接口的文档在线自动生成,降低后端开发人员编写接口文档的负担
- 功能测试
Spring已经将Swagger纳入自身的标准,建立了Spring-swagger项目,现在叫Springfox。通过在项目中引入Springfox ,即可非常简单快捷的使用Swagger。
knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案,前身是swagger-bootstrap-ui,取名knife4j是希望它能像一把匕首一样小巧,轻量,并且功能强悍!
目前,一般都使用knife4j框架。
-
项目中集成
-
导入依赖
导入 knife4j 的maven坐标(注意:由于knife4j是基于swagger的,所以也会自动导入swagger的依赖)
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
</dependency>
-
配置类
在配置类中加入 knife4j 相关配置,可以使knife4j在全局生效,目的就是项目中的所有接口都生成在线接口文档
在zzyl-framework工程中的config包下(无需大家编写,固定工具类,直接拷贝即可)
package com.zzyl.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.zzyl.properties.SwaggerConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.actuate.endpoint.web.*;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@Configuration
@EnableConfigurationProperties(SwaggerConfigProperties.class)
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {
@Autowired
SwaggerConfigProperties swaggerConfigProperties;
@Bean(value = "defaultApi2")
@ConditionalOnClass(SwaggerConfigProperties.class)
public Docket defaultApi2() {
// 构建API文档 文档类型为swagger2
return new Docket(DocumentationType.SWAGGER_2)
.select()
// 配置 api扫描路径
.apis(RequestHandlerSelectors.basePackage(swaggerConfigProperties.getSwaggerPath()))
// 指定路径的设置 any代表所有路径
.paths(PathSelectors.any())
// api的基本信息
.build().apiInfo(new ApiInfoBuilder()
// api文档名称
.title(swaggerConfigProperties.getTitle())
// api文档描述
.description(swaggerConfigProperties.getDescription())
// api文档版本
.version("1.0") // 版本
// api作者信息
.contact(new Contact(
swaggerConfigProperties.getContactName(),
swaggerConfigProperties.getContactUrl(),
swaggerConfigProperties.getContactEmail()))
.build());
}
/**
* 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题
**/
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier,
ServletEndpointsSupplier servletEndpointsSupplier,
ControllerEndpointsSupplier controllerEndpointsSupplier,
EndpointMediaTypes endpointMediaTypes,
CorsEndpointProperties corsProperties,
WebEndpointProperties webEndpointProperties,
Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
String basePath = webEndpointProperties.getBasePath();
EndpointMapping endpointMapping = new EndpointMapping(basePath);
boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(),
new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
}
private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}
}
在上述代码中引用了一个配置,用来定制项目中的一些特殊信息,比如扫描的包、项目相关信息等
@Setter
@Getter
@NoArgsConstructor
@ToString
@ConfigurationProperties(prefix = "zzyl.framework.swagger")
public class SwaggerConfigProperties implements Serializable {
/**
* 扫描的路径,哪些接口需要使用在线文档
*/
public String swaggerPath;
/**
* 项目名称
*/
public String title;
/**
* 具体描述
*/
public String description;
/**
* 组织名称
*/
public String contactName;
/**
* 联系网址
*/
public String contactUrl;
/**
* 联系邮箱
*/
public String contactEmail;
}
所以上述代码具体的配置,是在application.yml文件中来定义
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
zzyl:
framework:
swagger:
swagger-path: com.zzyl.controller
title: 智慧养老服务
description: 智慧养老
contact-name: 黑马研究院
contact-url: www.itheima.com
contact-email: itheima@itcast.cn
注意,在使用swagger时候,需要使用ant的方式进行匹配路径,需要设置为ant_path_matcher
Ant 是一种风格,简单匹配规则如下:
- ? 匹配一个字符
- * 匹配0个或多个字符
- ** 匹配0个或多个目录
-
静态资源映射
如果想要swagger生效,还需要设置静态资源映射,否则接口文档页面无法访问,
找到配置类为:WebMvcConfig,添加如下代码:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//支持webjars
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
//支持swagger
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
//支持小刀
registry.addResourceHandler("doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
}
-
常用注解
通过注解可以控制生成的接口文档,使接口文档拥有更好的可读性,常用注解如下:
注解
|
说明
|
@Api
|
用在类上,描述Controller的作用
|
@ApiOperation
|
用在方法上,说明方法的用途、作用
|
@ApiParam
|
用在方法的参数上,描述单个形参的含义
|
@ApiImplicitParam
|
用在方法上,描述单个形参的含义,与上面相比使用范围更广
|
@ApiModel
|
用在类上,用对象来接收参数或者返回参数,描述类的含义
|
@ApiModelProperty
|
用在类的属性上,用对象来接收参数或者返回参数,描述字段的含义
|
-
改造代码
改造我们刚才写的床位的接口BedController ,改造之后的代码如下:
package com.zzyl.controller;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.BedDto;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/bed")
@Api(tags = "床位管理相关接口")
public class BedController extends BaseController {
@Autowired
private BedService bedService;
@GetMapping("/read/room/{roomId}")
@ApiOperation(value = "根据房间id查询床位", notes = "传入房间id")
public ResponseResult<List<BedVo>> readBedByRoomId(
@ApiParam(value = "房间ID", required = true) @PathVariable("roomId") Long roomId) {
List<BedVo> beds = bedService.getBedsByRoomId(roomId);
return success(beds);
}
@PostMapping("/create")
@ApiOperation(value = "创建床位", notes = "传入床位对象,包括床位号和所属房间号")
public ResponseResult createBed(@RequestBody BedDto bedDto) {
bedService.addBed(bedDto);
return success();
}
@GetMapping("/read/{id}")
@ApiOperation(value = "根据id查询床位", notes = "传入床位id")
public ResponseResult<BedVo> readBed(@ApiParam(value = "床位ID", required = true) @PathVariable("id") Long id) {
BedVo bed = bedService.getBedById(id);
return success(bed);
}
@PutMapping("/update")
@ApiOperation(value = "更新床位", notes = "传入床位对象,包括床位id、床位号、所属房间号等信息")
public ResponseResult updateBed(@RequestBody BedDto bedDto) {
bedService.updateBed(bedDto);
return success();
}
@DeleteMapping("/delete/{id}")
@ApiOperation(value = "删除床位", notes = "传入床位id")
public ResponseResult deleteBed(@ApiParam(value = "床位ID", required = true) @PathVariable("id") Long id) {
bedService.deleteBedById(id);
return success();
}
}
BedDto 改造如下:
package com.zzyl.dto;
import com.zzyl.base.BaseDto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
@ApiModel("床位实体类")
public class BedDto extends BaseDto {
/**
* 床位编号
*/
@ApiModelProperty("床位编号")
private String bedNumber;
/**
* 床位状态: 未入住0, 已入住1
*/
@ApiModelProperty(value = "床位状态: 未入住0, 已入住1",example = "0")
private Integer bedStatus;
/**
* 房间ID
*/
@ApiModelProperty("房间ID")
private Long roomId;
/**
* 排序号
*/
@ApiModelProperty(value = "排序号")
private Integer sort;
}
其余的vo、dto、或者是controller都可以使用对应的注解,对接口进行描述
-
在线文档调试
代码改造完成之后,我们可以启动后端的服务,然后访问在线接口文档,访问地址:http://ip:port/doc.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了