当前位置: 首页 > news >正文

同时使用接口文档swagger和knife4j

项目场景:

springboot项目中同时使用接口文档swagger和knife4j


问题描述

在实体类中设置了字段必填的属性,在访问接口文档时出现异常

实体类关键代码片段

/*** 部门表 sys_dept*/
public class SysDept extends BaseEntity
{private static final long serialVersionUID = 1L;/** 部门ID */private Long deptId;/** 部门名称 */private String deptName;/** 显示顺序 */private Integer orderNum;@NotBlank(message = "部门名称不能为空")@Size(min = 0, max = 30, message = "部门名称长度不能超过30个字符")public String getDeptName(){return deptName;}public void setDeptName(String deptName){this.deptName = deptName;}@NotNull(message = "显示顺序不能为空")public Integer getOrderNum(){return orderNum;}public void setOrderNum(Integer orderNum){this.orderNum = orderNum;}
}

异常描述:

(1)没有加入knife4j配置,仅使用swagger配置

        <dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>3.0.0</version></dependency>
package com.pie.common.swagger.config;@Configuration
@EnableSwagger2
@EnableAutoConfiguration
@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
public class SwaggerAutoConfiguration
{/*** 默认的排除路径,排除Spring Boot默认的错误处理路径和端点*/private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**");private static final String BASE_PATH = "/**";@Bean@ConditionalOnMissingBeanpublic SwaggerProperties swaggerProperties(){return new SwaggerProperties();}@Beanpublic Docket api(SwaggerProperties swaggerProperties){// base-path处理if (swaggerProperties.getBasePath().isEmpty()){swaggerProperties.getBasePath().add(BASE_PATH);}// noinspection uncheckedList<Predicate<String>> basePath = new ArrayList<Predicate<String>>();swaggerProperties.getBasePath().forEach(path -> basePath.add(PathSelectors.ant(path)));// exclude-path处理if (swaggerProperties.getExcludePath().isEmpty()){swaggerProperties.getExcludePath().addAll(DEFAULT_EXCLUDE_PATH);}List<Predicate<String>> excludePath = new ArrayList<>();swaggerProperties.getExcludePath().forEach(path -> excludePath.add(PathSelectors.ant(path)));ApiSelectorBuilder builder = new Docket(DocumentationType.SWAGGER_2).host(swaggerProperties.getHost()).apiInfo(apiInfo(swaggerProperties)).select().apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()));swaggerProperties.getBasePath().forEach(p -> builder.paths(PathSelectors.ant(p)));swaggerProperties.getExcludePath().forEach(p -> builder.paths(PathSelectors.ant(p).negate()));return builder.build().securitySchemes(securitySchemes()).securityContexts(securityContexts()).pathMapping("/");}/*** 安全模式,这里指定token通过Authorization头请求头传递*/private List<SecurityScheme> securitySchemes(){List<SecurityScheme> apiKeyList = new ArrayList<SecurityScheme>();apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));return apiKeyList;}/*** 安全上下文*/private List<SecurityContext> securityContexts(){List<SecurityContext> securityContexts = new ArrayList<>();securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth()).operationSelector(o -> o.requestMappingPattern().matches("/.*")).build());return securityContexts;}/*** 默认的全局鉴权策略** @return*/private List<SecurityReference> defaultAuth(){AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];authorizationScopes[0] = authorizationScope;List<SecurityReference> securityReferences = new ArrayList<>();securityReferences.add(new SecurityReference("Authorization", authorizationScopes));return securityReferences;}private ApiInfo apiInfo(SwaggerProperties swaggerProperties){return new ApiInfoBuilder().title(swaggerProperties.getTitle()).description(swaggerProperties.getDescription()).license(swaggerProperties.getLicense()).licenseUrl(swaggerProperties.getLicenseUrl()).termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl()).contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail())).version(swaggerProperties.getVersion()).build();}
}

在这里插入图片描述
在查询接口以上两个字段deptNameorderNum不会被识别成必填字段

(2)在以上基础加入knife4j依赖

<!-- knife4j--><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.2</version></dependency>


在查询接口以上两个字段deptNameorderNum会被识别成必填字段。


原因分析:

应该是swagger对注解@NotBlank(message = "部门名称不能为空")没有生效。


解决方案:

目前还不知道为什么swagger没有对注解生效,也没有验证这个必填问题是否对前端接口调用有影响,期待大佬的解疑!!!

http://www.lryc.cn/news/384101.html

相关文章:

  • Compose - 权限申请
  • 第十九条:要么为继承而设计并提供文档说明,要么就禁止继承
  • Node.js全栈指南:浏览器显示一个网页
  • Linux远程桌面(Ubuntu/Deepin)——安装和使用 VNC 及通过 noVNC 实现浏览器实现远程桌面访问教程
  • 2024年最新通信安全员考试题库
  • SpringMVC系列八: 手动实现SpringMVC底层机制-下
  • 【昇思初学入门】第八天打卡-模型保存与加载
  • 喜报!极限科技新获得一项国家发明专利授权:“搜索数据库的正排索引处理方法、装置、介质和设备”
  • 深入探讨:UART与USART在单片机中串口的实际应用与实现技巧
  • Windows上PyTorch3D安装踩坑记录
  • 操作符详解(上) (C语言)
  • 使用 audit2allow 工具添加SELinux权限的方法
  • 一文弄懂FPGA
  • Rust 中使用 :: 这种语法的几种情况
  • Ruby langchainrb gem and custom configuration for the model setup
  • 高校新生如何选择最优手机流量卡?
  • QT QML 生成二维码
  • IDEA中Maven--下载安装自己适配的版本---理解
  • 【osgEarth】Ubuntu 22.04 源码编译osgEarth 3.5
  • ASP.NET Core 6.0 使用 资源过滤器和行为过滤器
  • 电脑屏幕花屏怎么办?5个方法解决问题!
  • git 初基本使用-----------笔记
  • Redis-数据类型-Bit的基本操作-getbit-setbit-Bitmap
  • 统信UOS上鼠标右键菜单中添加自定义内容
  • 学习入门 chatgpt原理 一
  • 生命在于学习——Python人工智能原理(4.7)
  • 经典游戏案例:仿植物大战僵尸
  • [Day 18] 區塊鏈與人工智能的聯動應用:理論、技術與實踐
  • 【Mac】DMG Canvas for mac(DMG镜像制作工具)软件介绍
  • RAG分块方法 从固定大小到自然语言处理分块——深入研究文本分块技术