SpringBoot整合Swagger-UI遇到的问题

SpringBoot整合Swagger-UI 遇到的问题

1.问题一:版本不兼容问题

在这里插入图片描述

解决办法:

(1)在.yml文件中改变匹配策略

  spring:
  mvc:
    path match:
      matching-strategy: ANT_PATH_MATCHER

(2)在spring中注入bean,放在swagger.config中

@Bean
public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
    return new BeanPostProcessor() {

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
                customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
            }
            return bean;
        }

        private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
            List<T> copy = mappings.stream()
                    .filter(mapping -> mapping.getPatternParser() == null)
                    .collect(Collectors.toList());
            mappings.clear();
            mappings.addAll(copy);
        }

        @SuppressWarnings("unchecked")
        private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
            try {
                Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
                field.setAccessible(true);
                return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
            } catch (IllegalArgumentException | IllegalAccessException e) {
                throw new IllegalStateException(e);
            }
        }
    };
}

2. 问题二:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘top.codingmore.mpg.mapper.Admin_role_relationMapper’ available: expected at least 1 bean which qualifies as autowire candidate

1.错误原因:mapper类没有成功注入(没有被扫描)
2.解决办法:在启动类上加上注解:@MapperScan({}), 括号内是mapper包所在的位置

3.问题三

Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.

原因:yml文件中没有配置数据源,配置一下数据源即可以解决。

datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/数据库名称?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot项目中整合Swagger UI,可以按照以下步骤进行操作: 1. 在项目的Maven或Gradle配置中添加Swagger依赖,例如: ```xml <!-- Maven --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` ```groovy // Gradle implementation 'io.springfox:springfox-boot-starter:3.0.0' ``` 2. 创建一个配置类(例如`SwaggerConfig`)并添加`@Configuration`和`@EnableSwagger2`注解,示例如下: ```java import springfox.documentation.swagger2.annotations.EnableSwagger2; import org.springframework.context.annotation.Configuration; @Configuration @EnableSwagger2 public class SwaggerConfig { // 配置内容 } ``` 3. 在配置类中,可以使用`Docket`来配置Swagger的基本信息,例如设置API的标题、描述等,示例如下: ```java import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import org.springframework.context.annotation.Bean; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controllers")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API Documentation") .description("API Documentation for my project") .version("1.0.0") .build(); } } ``` 4. 启动项目,访问`http://localhost:8080/swagger-ui.html`即可查看Swagger UI界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值