当mongodb模型中的int字段在数据库中具有空值时,Spring Boot抛出异常

ace*_*ace 0 mongoose mongodb spring-boot

我遇到一种情况,当用户没有为某个字段输入数据时,我将 null 作为该 int 字段的值传递,以表示用户没有输入任何值,例如

{user: 'John',
 age: null}
Run Code Online (Sandbox Code Playgroud)

但是当我在 Spring Boot 应用程序中阅读此类文档时,当它遇到上述文档时,会抛出

Caused by: java.lang.IllegalArgumentException: Parameter age must not be null!
Run Code Online (Sandbox Code Playgroud)

mongodb 中允许使用 null 值还是 Spring Boot 做错了什么?

我尝试过了:

@Data
@Document
public class User {
    final private String user;
    @org.springframework.lang.Nullable    
    final private int age;
}
Run Code Online (Sandbox Code Playgroud)

但这没有什么区别。除了不存储 null 之外(因为 null 已经填充在另一个节点/mongoose 应用程序中(它很乐意存储和读取 null 值,没有任何问题)使用相同的 mongodb 数据库),如何解决这个问题?

Rah*_*Raj 5

替换intInteger因为int是不接受null值的原始类型。Integer是一个接受值的包装对象null