SpringBoot e-Commerce project mall (35K + STAR) address: github.com/macrozheng/…

Abstract

Looking at the old Swagger API document style in the Mall project, I finally decided to take it up a notch this time. There were a lot of hiccups in the upgrade process, but if you use Maven properly, none of them are a problem!

Select the Upgrade Version

First we select the version we need to upgrade and go directly to the Maven repository to see which version is being used more often. Although the latest version 2.10.x is available, almost no one uses it, while the previous version 2.9.x is used by many people. It seems that 2.9.x is relatively stable, so we choose to upgrade to 2.9.2.

Upgrade Swagger

Then we can start to upgrade Swagger version, the original project used 2.7.0 version.

  • Due to themallThe project uses the parent project to manage dependencies uniformly, so just modify the Swagger dependency version in the parent project. The pom.xml of the parent project is in the root directory of the project.
<properties>
    <swagger2.version>2.9.2</swagger2.version>
</properties>
Copy the code
  • runmall-adminThe project cannot be started, and the error message is as follows: there is a dependency in which a method cannot be foundguavaInside, it is estimated that the version of the problem;
*************************** 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 guava / 18.0-18.0. The jar! /com/google/common/collect/FluentIterable.class It was loaded from the following location: File: / C: / Users/macrozheng/m2 / repository/com/Google / / guava guava guava / 18.0-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 1Copy the code
  • When there are several dependencies that use different versions ofguavaHow does Maven select packages? Maven is selected on the proximity principle. The shallower the dependency level, the more likely it is to be selected.

  • It is recommended to useMaven HelperThis IDEA plug-in, directly viewmall-adminWhether the project has dependency conflicts, and guava does;

  • It can be found by observationminioThis dependency level is the shallowest, so we use the Guava version of it.
<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>guava</artifactId>
            <groupId>com.google.guava</groupId>
        </exclusion>
    </exclusions>
</dependency>
Copy the code
  • After the exclusion is complete, we find that guava’s dependency conflict has disappeared, and run againmall-adminThe project has been found to be running properly;

  • When we access Swagger file, we find a 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)
Copy the code
  • The reason is when we use the @APIModelProperty annotation, as the Long data type, if you don’t add itexampleProperty, which defaults to an empty string, and NumberFormatException is naturally raised for empty string transformations;
** * Created by macro on 2018/10/29. */
@Getter
@Setter
public class OmsMoneyInfoParam {
    @ApiModelProperty(value = "Order ID",example = "1")
    private Long orderId;
}
Copy the code
  • We’ve already used many @apiModelProperty annotations, so it’s not possible to add them one by one, but use the new versionswagger-annotationsandswagger-modelsThe dependency package can be solved, so our Swagger dependency becomes the following;
<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>
    <! 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>
Copy the code
  • Is it really a good idea to run mall-admin again and see that the problem has been fixed. If we find inappropriate dependencies in Maven, we will remove them and then introduce the appropriate version of the dependency.

  • In fact, we can take advantage of Maven project inheritance features, directly in the parent project to specify the dependency version, so that the dependency version of the child project can be unified;

  • XML file in the root directory and specify the version number.

<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>
Copy the code
  • Add the dependencies that need to be managed uniformly under the dependency management node of the parent project, so the Swagger version upgrade is completed.
<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>
        <! 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>
        <! -- Unified Guava version to prevent conflicts -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>${guava.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>
Copy the code
  • When the Token access interface is configured, the interface under the brand, product, and product category has the permission to access, but the other interfaces have no permission. That is because the following configuration is used to configure the path requiring login authentication.
@Configuration
@EnableSwagger2
public class Swagger2Config {

    private List<SecurityContext> securityContexts(a) {
        // Set the path for login authentication
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/brand/.*"));
        result.add(getContextByPath("/product/.*"));
        result.add(getContextByPath("/productCategory/.*"));
        returnresult; }}Copy the code
  • This is a bit different from the old version. The old version carries the Token in the header for accessing all interfaces. The new version only carries the Token for the configured path.
@Configuration
@EnableSwagger2
public class Swagger2Config {
    private List<SecurityContext> securityContexts(a) {
        // Set the path for login authentication
        List<SecurityContext> result = new ArrayList<>();
        result.add(getContextByPath("/ / *. *"));
        returnresult; }}Copy the code

The interface comparison between the new version and the old version

Swagger has been upgraded to version 2.9.2 and the interface has been instantly beautiful. Let’s take a look at the old interface.

The old version

The new version

Project source code address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.