springboot2.3.0.升级 2.6.7

目录

      • 2.3.0. 主要版本替换 2.6.7
        • 1、集成 swagger 升级 , 增加用户密码,进入页面 没有发现 注解内容
        • 2、启动一直打印swagger日志
        • 3、去除 ribbon ,增加 rest 支持
        • 4、引入 grpc 组件,项目启动失败

2.3.0. 主要版本替换 2.6.7

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!--  <version>2.3.0.RELEASE</version>  -->
    <version>2.6.7</version>
    <relativePath/>
</parent>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <!--            <version>Hoxton.SR12</version>-->
            <version>2021.0.7</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
1、集成 swagger 升级 , 增加用户密码,进入页面 没有发现 注解内容

升级版本

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

增加 用户名和密码配置
Knife4j配置

knife4j:
  # 是否开启增强模式
  enable: true  #是否开启Swagger
  basic:
    enable: true  #进入界面是否需要账号密码
    username: cas
    password: cas123

代码配置

@EnableSwagger2
@Configuration
@Profile({"dev", "pre", "prod"})
public class Swagger2Config {

    /**
     * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(this.apiInfo()).select()
            // .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
            .apis(RequestHandlerSelectors.basePackage("com.br.auth.controller"))
            // 为当前包路径
            .paths(PathSelectors.any()).build();
    }

    /**
     * 构建 api文档的详细信息函数
     */
    private ApiInfo apiInfo() {
        ApiInfo apiInfo = new ApiInfoBuilder()
            // 页面标题
            .title("cas-API文档")
            // 创建人
            .contact(new Contact("account-auth", "http://cas.100credit.cn", "[email protected]"))
            // 版本号
            .version("1.0")
            // 描述
            .description("cas接口").build();
        return apiInfo;
    }

    @Bean
    public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
        return new BeanPostProcessor() {
            @Override
            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
                if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
                    this.customizeSpringfoxHandlerMappings(this.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);
                }
            }
        };
    }
}

坑 1: 进入页面 没有发现 注解内容,多方排查 ,需要增加

spring.mvc.pathmatch.matching-strategy: ant_path_matcher 

这个配置 ,
https://zhuanlan.zhihu.com/p/447338078

2、启动一直打印swagger日志

2024-01-09 17:36:01.847 [account-auth] [main] WARN [springfox.documentation.swagger.readers.operation.OperationImplicitParameterReader] -[traceId-]-Unable to interpret the implicit parameter configuration with dataType: int, dataTypeClass: class java.lang.Void

swager 升级为 3.0 版以上问题

在所有使用@ApiImplicitParam注解的地方,加上dataTypeClass的值。

参考:https://blog.csdn.net/m290345792/article/details/123126937

@ApiImplicitParam(name = "roleId", value = "角色id", paramType = "query", required = true, dataType = "int",
    dataTypeClass = Integer.class)


@ApiImplicitParam(name = "projectCode", value = "项目编号", paramType = "query", required = true,
    dataType = "String", dataTypeClass = String.class),
3、去除 ribbon ,增加 rest 支持

spring 2.6.4 去除了对 ribbon 的支持,需要全部移除 ribbon 组件,使用

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

替换

增加

@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
return restTemplate;
}

修改

@FeignClient(name = "crm-service", url = "${rest.crm-service}")
// @FeignClient("crm-service")
public interface CrmInfoFeign {
4、引入 grpc 组件,项目启动失败
<!--grpc公共包-->
<dependency>
    <groupId>com.br</groupId>
    <artifactId>br-grpc-common</artifactId>
    <version>1.0.5</version>
</dependency>

该组件,里面引用了 boot1.5.4 ,引用了 ribbon ,需要去除

<!--grpc公共包-->
<dependency>
    <groupId>com.br</groupId>
    <artifactId>br-grpc-common</artifactId>
    <version>1.0.5</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-web</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-boot</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-boot-starter</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-boot-starter-aop</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-core</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <groupId>org.springframework.cloud</groupId>
        </exclusion>
        <exclusion>
            <artifactId>logback-core</artifactId>
            <groupId>ch.qos.logback</groupId>
        </exclusion>
        <exclusion>
            <artifactId>logback-classic</artifactId>
            <groupId>ch.qos.logback</groupId>
        </exclusion>
        <exclusion>
            <artifactId>lombok</artifactId>
            <groupId>org.projectlombok</groupId>
        </exclusion>
        <exclusion>
            <artifactId>google-http-client-jackson2</artifactId>
            <groupId>com.google.http-client</groupId>
        </exclusion>
        <exclusion>
            <artifactId>error_prone_annotations</artifactId>
            <groupId>com.google.errorprone</groupId>
        </exclusion>
        <exclusion>
            <artifactId>grpc-stub</artifactId>
            <groupId>io.grpc</groupId>
        </exclusion>
        <exclusion>
            <artifactId>grpc-context</artifactId>
            <groupId>io.grpc</groupId>
        </exclusion>
        <exclusion>
            <artifactId>guava</artifactId>
            <groupId>com.google.guava</groupId>
        </exclusion>
        <exclusion>
            <artifactId>opencensus-contrib-http-util</artifactId>
            <groupId>io.opencensus</groupId>
        </exclusion>
        <exclusion>
            <artifactId>google-http-client</artifactId>
            <groupId>com.google.http-client</groupId>
        </exclusion>
        <exclusion>
            <artifactId>okhttp</artifactId>
            <groupId>com.squareup.okhttp</groupId>
        </exclusion>
        <exclusion>
            <artifactId>proto-google-common-protos</artifactId>
            <groupId>com.google.api.grpc</groupId>
        </exclusion>
        <exclusion>
            <artifactId>simpleclient_hotspot</artifactId>
            <groupId>io.prometheus</groupId>
        </exclusion>
        <exclusion>
            <artifactId>simpleclient_httpserver</artifactId>
            <groupId>io.prometheus</groupId>
        </exclusion>
        <exclusion>
            <artifactId>simpleclient_common</artifactId>
            <groupId>io.prometheus</groupId>
        </exclusion>
    </exclusions>
</dependency>

你可能感兴趣的:(#,SpringBoot,springboot,spring,cloud)