关于java:给Swagger升级了新版本没想到居然有这么多坑

3次阅读

共计 5469 个字符,预计需要花费 14 分钟才能阅读完成。

SpringBoot 实战电商我的项目 mall(35k+star)地址:https://github.com/macrozheng/mall

摘要

看着 mall 我的项目中古老的 Swagger API 文档款式,这次我终于下定决心要给它升个级了。降级过程中遇到了好多坑,不过只有用好 Maven,这些都不是个事!

抉择降级版本

首先咱们抉择下须要降级的版本,间接去 Maven 仓库看下,哪个版本应用的比拟多。尽管有最新版本 2.10.x,然而简直没什么人用,而上一个版本2.9.x 应用的人却很多,看样子还是 2.9.x 版本比较稳定,咱们抉择降级到 2.9.2 版本。

降级 Swagger

接下来咱们就能够开始降级 Swagger 版本了,原来我的项目里用的是 2.7.0 版本。

  • 因为 mall 我的项目应用父我的项目来对立治理依赖,所以只有批改父我的项目中的 Swagger 依赖版本即可,父我的项目的 pom.xml 在我的项目根目录下;
<properties>
    <swagger2.version>2.9.2</swagger2.version>
</properties>
  • 运行 mall-admin 我的项目发现无奈启动,报错信息如下,有个依赖外面的某个办法找不到了,一看是 guava 外面的,预计是版本的问题;
***************************
APPLICATION FAILED TO START
***************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    springfox.documentation.schema.DefaultModelDependencyProvider.dependentModels(DefaultModelDependencyProvider.java:79)

The following method did not exist:

    com.google.common.collect.FluentIterable.concat(Ljava/lang/Iterable;Ljava/lang/Iterable;)Lcom/google/common/collect/FluentIterable;

The method's class, com.google.common.collect.FluentIterable, is available from the following locations:

    jar:file:/C:/Users/macrozheng/.m2/repository/com/google/guava/guava/18.0/guava-18.0.jar!/com/google/common/collect/FluentIterable.class

It was loaded from the following location:

    file:/C:/Users/macrozheng/.m2/repository/com/google/guava/guava/18.0/guava-18.0.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of com.google.common.collect.FluentIterable


Process finished with exit code 1
  • 当有好几个依赖都应用了不同版本的 guava 包时,Maven 是如何抉择的呢?Maven 是依照就近准则抉择的,层级越是浅的依赖越会被抉择;

  • 此时举荐应用 Maven Helper 这款 IDEA 插件,间接查看 mall-admin 我的项目是否存在依赖抵触,guava 版本果然抵触了;

  • 通过观察能够发现 minio 这个依赖层级最浅,所以应用的是它的 guava 版本,间接排除掉即可;
<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>guava</artifactId>
            <groupId>com.google.guava</groupId>
        </exclusion>
    </exclusions>
</dependency>
  • 排除实现后发现 guava 的依赖抵触曾经不见了,再次运行 mall-admin 我的项目,发现曾经能够失常运行了;

  • 当咱们拜访 Swagger 文档时,又发现了一个问题,会报 NumberFormatException 异样;
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Long.parseLong(Long.java:601)
    at java.lang.Long.valueOf(Long.java:803)
    at io.swagger.models.parameters.AbstractSerializableParameter.getExample(AbstractSerializableParameter.java:412)
  • 起因是当咱们应用 @ApiModelProperty 注解时,作为 Long 数据类型,如果你不增加 example 属性,默认值是空字符串,空字符串转型天然就会报 NumberFormatException 异样;
/**
 * 批改订单费用信息参数
 * Created by macro on 2018/10/29.
 */
@Getter
@Setter
public class OmsMoneyInfoParam {@ApiModelProperty(value = "订单 ID",example = "1")
    private Long orderId;
}
  • 咱们曾经应用了很多 @ApiModelProperty 注解,要一个个增加那是不可能的,不过应用新版本的 swagger-annotationsswagger-models依赖包就能够解决了,于是咱们的 Swagger 依赖变成了上面这样的;
<dependencies>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <exclusions>
            <exclusion>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-annotations</artifactId>
            </exclusion>
            <exclusion>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-models</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
    </dependency>
    <!-- 解决 Swagger 2.9.2 版本 NumberFormatException-->
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-models</artifactId>
        <version>1.6.0</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>1.6.0</version>
    </dependency>
</dependencies>
  • 再次运行 mall-admin 发现该问题曾经解决了,咱们在 maven 中一发现不适合的依赖就排除掉,而后引入适合版本的依赖,这样做真的好么?
  • 其实咱们能够利用 Maven 我的项目的继承个性,间接在父我的项目中规定好依赖的版本,这样子我的项目的依赖版本就能对立了;
  • 先把原来 pom.xml 中排除 guava 和 swagger 的配置给去除了,而后批改根目录下的 pom.xml 文件,指定版本号;
<properties>
    <swagger2.version>2.9.2</swagger2.version>
    <swagger-models.version>1.6.0</swagger-models.version>
    <swagger-annotations.version>1.6.0</swagger-annotations.version>
    <guava.version>20.0</guava.version>
</properties>
  • 在父我的项目的依赖治理节点下增加须要对立治理的相干依赖,至此 Swagger 版本升级实现;
<dependencyManagement>
    <dependencies>
        <!--Swagger-UI API 文档生产工具 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger2.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger2.version}</version>
        </dependency>
        <!-- 解决 Swagger 2.9.2 版本 NumberFormatException-->
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>${swagger-models.version}</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>${swagger-annotations.version}</version>
        </dependency>
        <!-- 对立 Guava 版本避免抵触 -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
  • 当咱们配置好 Token 拜访须要权限的接口时,会发现品牌、商品、商品分类下的接口有权限拜访,其余提醒无权限,那是因为咱们应用了如下配置来配置须要登录认证的门路;
@Configuration
@EnableSwagger2
public class Swagger2Config {private List<SecurityContext> securityContexts() {
        // 设置须要登录认证的门路
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/brand/.*"));
        result.add(getContextByPath("/product/.*"));
        result.add(getContextByPath("/productCategory/.*"));
        return result;
    }
}
  • 批改为全副门路即可,这个和旧版有点不同,旧版拜访所有接口都会在头信息中带 Token,而新版只会对配置的门路带 Token。
@Configuration
@EnableSwagger2
public class Swagger2Config {private List<SecurityContext> securityContexts() {
        // 设置须要登录认证的门路
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/*/.*"));
        return result;
    }
}

新老版本界面比照

Swagger 降级到 2.9.2 版本后界面霎时变得好看了,让咱们对新老界面来个比照。

老版本

新版本

我的项目源码地址

https://github.com/macrozheng…

公众号

mall 我的项目全套学习教程连载中,关注公众号 第一工夫获取。

正文完
 0