瑞吉外卖项目学习条记(二)Swagger、logback、表单校验和参数打印功能的实现

[复制链接]
发表于 2025-12-2 07:42:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
瑞吉外卖项目学习条记(一)准备工作、员工登录功能实现

  
3 项目组件优化

3.1 实现Swagger文档输出


      
  • 1)在application.yml中增长knife4j设置
  1. spring:
  2.   mvc:
  3.     pathmatch:
  4.       matching-strategy: ANT_PATH_MATCHER
  5. knife4j:
  6.   enable: true
  7.   title: 瑞吉外卖
  8.   group: ruiji_takeout
  9.   description: 瑞吉外卖
  10.   version: 1.0
  11.   name: itweid
  12.   url:
  13.   email:
  14.   base-package: com.itweid.takeout.controller
复制代码

      
  • 2)创创建置类SwaggerProperties类吸取设置
  1. @Data
  2. @ConfigurationProperties(prefix = "knife4j")
  3. public class SwaggerProperties {
  4.    
  5.    
  6.     private String title = ""; //标题
  7.     private String group = ""; //组名
  8.     private String description = ""; //描述
  9.     private String version = ""; //版本
  10.     private String name = ""; // 联系人
  11.     private String url = ""; // 联系人url
  12.     private String email = ""; // 联系人email
  13.     private String basePackage = ""; //swagger会解析的包路径
  14.     private List<String> basePath = new ArrayList<>(); //swagger会解析的url规则
  15.     private List<String> excludePath = new ArrayList<>(); //在basePath基础上需要排除的url
  16.     // 如果没有填写组名,则直接用标题作为组名
  17.     public String getGroup() {
  18.    
  19.    
  20.         if (group == null || group.isEmpty()) {
  21.    
  22.    
  23.             return title;
  24.         }
  25.         return group;
  26.     }
  27. }
复制代码

      
  • 3)创建自动设置类SwaggerAutoConfiguration举行初始化
  1. @Configuration
  2. @ConditionalOnProperty(name = "knife4j.enable", havingValue = "true", matchIfMissing = true)
  3. @EnableSwagger2
  4. @EnableConfigurationProperties(SwaggerProperties.class)
  5. public class SwaggerAutoConfiguration implements BeanFactoryAware {
  6.    
  7.    
  8.     @Autowired
  9.     private SwaggerProperties swaggerProperties;
  10.     @Autowired
  11.     private BeanFactory beanFactory;
  12.     @Bean
  13.     @ConditionalOnMissingBean
  14.     public List<Docket> createRestApi(){
  15.    
  16.    
  17.         ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
  18.         List<Docket> docketList = new LinkedList<>();
  19.         ApiInfo apiInfo = new ApiInfoBuilder()
  20.                 // 页面标题
  21.                 .title(swaggerProperties.getTitle())
  22.                 // 创建人
  23.                 .contact(new Contact(swaggerProperties.getName(),
  24.                         swaggerProperties.getUrl(),
  25.                         swaggerProperties.getEmail()))
  26.                 // 版本号
  27.                 .version(swaggerProperties.getVersion())
  28.                 // 描述
  29.                 .description(swaggerProperties.getDescription())
  30.                 .build();
  31.         Docket docket = new Docket(DocumentationType.SWAGGER_2)
  32.                 .apiInfo(apiInfo)
  33.                 .groupName(swaggerProperties.getGroup())
  34.                 .select()
  35.                 // 为当前包路径
  36.                 .apis(RequestHandlerSelectors.
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表