Java记录验证注释

huangapple go评论86阅读模式
英文:

Java records validation anotation

问题

以下是翻译好的内容:

我有一个带有有效注释的旧POJO:

public class SomeData {

    @NotNull(message = "数据不能为空")
    private String data;

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

    public String getData() {
        return data;
    }

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

但是现在我有了records

public record SomeData(String data) {}

我如何在records中添加注释?

英文:

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

得分: 19

你可以这样做:

public record SomeData(@NotNull(message = "数据不能为空") String data) {}
英文:

You can do like that:

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

huangapple
  • 本文由 发表于 2020年10月4日 01:16:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/64186922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定