马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
瑞吉外卖项目学习条记(一)准备工作、员工登录功能实现
3 项目组件优化
3.1 实现Swagger文档输出
- 1)在application.yml中增长knife4j设置
- spring:
- mvc:
- pathmatch:
- matching-strategy: ANT_PATH_MATCHER
- knife4j:
- enable: true
- title: 瑞吉外卖
- group: ruiji_takeout
- description: 瑞吉外卖
- version: 1.0
- name: itweid
- url:
- email:
- base-package: com.itweid.takeout.controller
复制代码
- 2)创创建置类SwaggerProperties类吸取设置
- @Data
- @ConfigurationProperties(prefix = "knife4j")
- public class SwaggerProperties {
-
-
- private String title = ""; //标题
- private String group = ""; //组名
- private String description = ""; //描述
- private String version = ""; //版本
- private String name = ""; // 联系人
- private String url = ""; // 联系人url
- private String email = ""; // 联系人email
- private String basePackage = ""; //swagger会解析的包路径
- private List<String> basePath = new ArrayList<>(); //swagger会解析的url规则
- private List<String> excludePath = new ArrayList<>(); //在basePath基础上需要排除的url
- // 如果没有填写组名,则直接用标题作为组名
- public String getGroup() {
-
-
- if (group == null || group.isEmpty()) {
-
-
- return title;
- }
- return group;
- }
- }
复制代码
- 3)创建自动设置类SwaggerAutoConfiguration举行初始化
- @Configuration
- @ConditionalOnProperty(name = "knife4j.enable", havingValue = "true", matchIfMissing = true)
- @EnableSwagger2
- @EnableConfigurationProperties(SwaggerProperties.class)
- public class SwaggerAutoConfiguration implements BeanFactoryAware {
-
-
- @Autowired
- private SwaggerProperties swaggerProperties;
- @Autowired
- private BeanFactory beanFactory;
- @Bean
- @ConditionalOnMissingBean
- public List<Docket> createRestApi(){
-
-
- ConfigurableBeanFactory configurableBeanFactory = (ConfigurableBeanFactory) beanFactory;
- List<Docket> docketList = new LinkedList<>();
- ApiInfo apiInfo = new ApiInfoBuilder()
- // 页面标题
- .title(swaggerProperties.getTitle())
- // 创建人
- .contact(new Contact(swaggerProperties.getName(),
- swaggerProperties.getUrl(),
- swaggerProperties.getEmail()))
- // 版本号
- .version(swaggerProperties.getVersion())
- // 描述
- .description(swaggerProperties.getDescription())
- .build();
- Docket docket = new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo)
- .groupName(swaggerProperties.getGroup())
- .select()
- // 为当前包路径
- .apis(RequestHandlerSelectors.
复制代码 |