ace
ace

Reputation: 12024

Spring Boot throws exception when int field in mongodb model has null value in the database

I have a situation when a user does not enter data for certain field I pass null as value for that int field to signify that no value was entered by the user like

{user: 'John',
 age: null}

but when I read such document in my Spring Boot application, when it encounters above document, throws

Caused by: java.lang.IllegalArgumentException: Parameter age must not be null!

Is null allowed value in mongodb or Spring Boot is doing something wrong?

I tried to:

@Data
@Document
public class User {
    final private String user;
    @org.springframework.lang.Nullable    
    final private int age;
}

But it makes no difference. How to solve this problem other than not storing null ( because null is already populated in another node/mongoose application (which gladly stores and reads null values without any issue) using the same mongodb database?

Upvotes: 1

Views: 2293

Answers (1)

Rahul Raj
Rahul Raj

Reputation: 3459

Replace int with Integer since int is a primitive type which won't accept null values. Integer is a wrapper object that accepts null values.

Upvotes: 7

Related Questions