首先引入springboot包 compile group: 'org.springframework.boot', name: 'spring-boot-starter-validation' 然后创建一个实体类 @Setter @Getter public class CountryBo { @NotNull(message = "{region.error.null}") @Length(max = 10,message = "{region.error.length}") private String region; @NotNull(message = "{countryCode.error.null}") @Length(max = 10,message = "{countryCode.error.length}") private String countryCode; } 这里使用了消息文件直接把ValidationMessages.properties文件放到resource目录下,也可通过springboot的配置自定义文件名 region.error.null=region is not allow null region.error.length=More than the length of region countryCode.error.null=region is not allow null countryCode.error.length=More than the length of region id.error.null=id is not allo