javarecordjava-14

Java records validation anotation


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?


Solution

  • You can do like that:

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