Java records validation anotation

18.1k views Asked by At

I have old POJO with valid annotation:

public class SomeData {

    @NotNull(message = "Data not be null")
    private String data;

    public FormData(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }

   public String setData(String data) {
        this.data = data
    }
    
}

But know I have records

public record SomeData(String data) {}

How I can add annotation in records?

1

There are 1 answers

3
deikyb On BEST ANSWER

You can do like that:

public record SomeData(@NotNull(message = "Data not be null") String data) {}