# 项目错误记录
近日在开发SpringBoot项目时,遇到有关Swaagger-ui的错误。值得记录一下。在访问swagger-ui页面时入到如下错误:
```
.AbstractSerializableParameter | 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)
at sun.reflect.GeneratedMethodAccessor200.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:688)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:721)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:166)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
```
准确地说,这并不是一个错误,它只是一条警告日志消息。根据基本判断,可能是相关`Annotation`的`Description`错误。例如(eg), `@ApiModelProperty(value="${property1.description}")`这样才是正确的。
因此,我们需要修复相关的代码。首先,需要检查任何`@APiModelProerty`注释。必须在任何数字类型上设置示例值并使用注释`@ApiModelProperty`,如下所示:
```java
@ApiModelProperty(notes = "联系人的唯一标识符。",
example = "1", required = true, position = 0)
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
```
但这还不够,还需要为使用`@ApiParam`注释并具有数字类型的字段类型的每个参数设置示例值。如下所示:
```java
public ResponseEntity<Contact> findContactById(
@ApiParam(name = "contactId",
value = "要获取的联系人的Id。不能为空。",
example = "1",
required = true)
@PathVariable long contactId) {
. ..
return ...
}
```
所以,我的`ContactController`的变化是:
![image-20220428203754944](https://mybolg-typora.oss-cn-beijing.aliyuncs.com/image-20220428203754944.png)
完善`@ApiParam`的注释部分即可。
# Project error record
I recently encountered an error with swaagger-UI while developing the SpringBoot project. It's worth noting. When accessing swagger- UI, I get the following error:
```
.AbstractSerializableParameter | 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)
at sun.reflect.GeneratedMethodAccessor200.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:688)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:721)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:166)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:119)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
```
This isn't exactly an error, it's just a warning log message. Depending on the basic judgment, it may be a `Description` error for the relevant `Annotation`. For example , `@ApiModelProperty (value="${property1.description}")` is described correctly.
Therefore, we need to fix the relevant code. First, you need to check for any `@ApiModelProerty` annotations. Example values must be set on any numeric type with the annotation `@ApiModelProperty` as follows:
```java
@APIModelProperty (Notes = "Unique identifier of the contact." .
example = "1", required = true, position = 0)
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
```
But that's not enough. You also need to set sample values for each parameter of a field type that uses the `@ApiParam` annotation and has a numeric type. As follows:
```java
public ResponseEntity<Contact> findContactById(
@ApiParam(name = "contactId",
Value = "Id of the contact to be obtained. It cannot be empty." .
example = "1",
required = true)
@PathVariable long contactId) {
...
Return to...
}
```
So, the change to my `ContactController` is:
![image-20220428203754944](https://mybolg-typora.oss-cn-beijing.aliyuncs.com/image-20220428203754944.png)
Perfect the comment part of `@ApiParam`. Check all those `@ApiParam` with example. Now, that warning gone...
Swagger-ui project error record