Gaurav Agrawal
Gaurav Agrawal

Reputation: 47

Is there any Spring Annotation to Set Default value for a Field (Mongo)?

Is there any Spring Annotation to Set Default value for a Field (Mongo) ?

Upvotes: 9

Views: 17701

Answers (2)

Gaurav Raghav
Gaurav Raghav

Reputation: 187

You have to tell your Builder to use default value for some field using @Builder.Default(annotation).

Like this,

    @Builder
    @Document
    public class Document {

    @Builder.Default
    private String field = "any_value"; 

    }

Upvotes: 1

felix
felix

Reputation: 9285

No need for spring annotations, this should do the trick:

     import org.springframework.data.mongodb.core.mapping.Document;
     import org.springframework.data.mongodb.core.mapping.Field;


    @Document
    public class Doc {

    @Field
    private String field = "CustomDefaultValue"; 

    }

Upvotes: 21

Related Questions