Solve the problem of type conversion error of swagger

Today, it was found that when the project was started, a pile of error reports would be printed. scared.

After checking, I found the error prompt of swagger. It does not affect the function. But looking at a pile of mistakes is also disturbing, isn't it?

So we still have to solve it.

2020-11-04 10:50:46,383 [http-nio-9292-exec-3] WARN i.s.m.parameters.AbstractSerializableParameter:421 - Illegal DefaultValue null for parameter type integer
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)

By looking at the source code of line abstractserializable parameter 412

          if (BaseIntegerProperty.TYPE.equals(type)) {
              return Long.valueOf(example);

It is found that if the attribute type is Integer, it is converted to Long

The default value of example is' ', which leads to conversion errors.

The first approach

Therefore, solution 1 is obtained:

In the entity class, when adding @ ApiModelProperty to the attribute of Integer type, you must assign a value to the example parameter, and the value must be of numeric type.

@ApiModelProperty(value = "", example = "0")
private Integer id;

However, if there are a lot of modification points in the project, it will be too tired to make changes.

The second approach

Then look at the code

      if (example == null) {
          return null;
      }

There is a null judgment in front. If you add a "" empty string judgment, it will actually be ok

However, we need to download the source code of swagger and package and release it ourselves.

Then replace the official version

The third approach

This can be regarded as a small bug in swagger. I saw it on the Internet and said that the problem is End swim Swagger models: bug in 1.5.20.

Look at me. This is really this version.

This problem has been modified in version 1.5.21, so we can also solve it by replacing dependencies

Swagger models are dependent on springfox-swagger2

<dependency>
              <groupId>io.springfox</groupId>
              <artifactId>springfox-swagger2</artifactId>
              <version>2.9.2</version>
          </dependency>

Check his dependencies. It's version 1.5.20

Then I checked the latest 3.0.0 version of springfox-swagger2, which still relies on swagger models of 1.5.20

So you can only specify the version of swagger models manually. In other words, even if the higher version of swagger2 relies on 1.5.21 models, I dare not upgrade springfox-swagger2 directly. After all, this action is a little big. We need to do a comprehensive regression test before we dare to produce.

<!--Swagger-UI API Document production tools-->
<dependency>
  <groupId>io.swagger</groupId>
  <artifactId>swagger-models</artifactId>
  <version>1.5.21</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>

By manually adding the dependency of swagger models version 1.5.21 before the spring fox-swagger2 dependency

According to maven's dependency principle, under the same path length, whoever declares first has priority. Put 1.5.21 on it to exclude the 1.5.20 version of springfox-swagger2 dependency

There is no need to add exclusions manually

The third approach is recommended. I'll send the version test here, ok.

Keywords: Java RESTful

Added by jumpenjuhosaphat on Fri, 12 Nov 2021 14:21:52 +0200