javax.validation to validate list of values?

前端 未结 3 741
南笙
南笙 2020-12-28 13:06

is there a way to use javax.validation to validate a variable of type string called colour that needs to have these values only(red, blue, green, pink) using annotations?

3条回答
  •  清歌不尽
    2020-12-28 13:45

    you can create an enum

    public enum Colors {
        RED, PINK, YELLOW
    }
    

    and then in your model, you can validate it like so:

    public class Model {
        @Enumerated(EnumType.STRING)
        private Colors color;
    }
    

    which will validate your payload against the enum, given that you have added @Valid in your RestController.

提交回复
热议问题