简体   繁体   中英

Default value for Lombok @Value without @Builder

Is there a way to provide default value for fields under model annotated with @Value . For example:

@Value
class Book {
  String title;
  List<String> authors;
}

In above model, Jackson deserializes authors to null from json payload that has authors as null , but I want author to have a [] as default value as opposed to null . I know below won't work because field becomes private final due to @Value

  List<String> authors = Collections.emptyList();

I could override the all args constructor but the model itself is quite large so I am looking for something similar to Builder.Default but for @Value .

You can add your own constructor which initializes your authors field.

@Value
@AllArgsConstructor
static class Book {
    String title;
    List<String> authors;

    public Book(String title) {
        this(title, Collections.emptyList());
    }
}

@AllArgsConstructor was required as, when a custom constructor was added, @Value skipped all field constructor generation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM