下面列出了怎么用javax.validation.constraints.NegativeOrZero的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Determine a number type's maximum (inclusive) value.
*
* @param member the field or method to check
* @return specified inclusive maximum value (or null)
* @see Max
* @see DecimalMax#inclusive()
* @see NegativeOrZero
*/
protected BigDecimal resolveNumberInclusiveMaximum(MemberScope<?, ?> member) {
Max maxAnnotation = this.getAnnotationFromFieldOrGetter(member, Max.class, Max::groups);
if (maxAnnotation != null) {
return new BigDecimal(maxAnnotation.value());
}
DecimalMax decimalMaxAnnotation = this.getAnnotationFromFieldOrGetter(member, DecimalMax.class, DecimalMax::groups);
if (decimalMaxAnnotation != null && decimalMaxAnnotation.inclusive()) {
return new BigDecimal(decimalMaxAnnotation.value());
}
NegativeOrZero negativeAnnotation = this.getAnnotationFromFieldOrGetter(member, NegativeOrZero.class, NegativeOrZero::groups);
if (negativeAnnotation != null) {
return BigDecimal.ZERO;
}
return null;
}
@Override
public PropertyModel postProcessInternal(PropertyModel propertyModel) {
NegativeOrZero negativeAnno = propertyModel.getPropertyItem().getAnnotation(NegativeOrZero.class);
if (negativeAnno == null) return propertyModel;
propertyModel.setDescription(TextUtils.combine(
propertyModel.getDescription(),
" (值只能为负数或0)"
));
return propertyModel;
}
@NegativeOrZero(groups = Test.class)
public Long getNegativeOrZeroOnGetterLong() {
return negativeOrZeroOnGetterLong;
}
@GetMapping(value = "/persons5")
public String persons5(@NegativeOrZero int age) {
return "OK";
}
private String resolveRange(Field field) {
String min = Optional.ofNullable(field.getAnnotation(Min.class)).map((item) -> String.valueOf(item.value())).orElse(null);
if (min == null) {
min = Optional.ofNullable(field.getAnnotation(DecimalMin.class)).map(DecimalMin::value).orElse(null);
}
if (field.isAnnotationPresent(PositiveOrZero.class)) {
if (min == null || new BigDecimal(min).compareTo(BigDecimal.ZERO) < 0) {
min = "非负数";
}
}
if (field.isAnnotationPresent(Positive.class)) {
if (min == null || new BigDecimal(min).compareTo(BigDecimal.ZERO) <= 0) {
min = "正数";
}
}
min = min == null ? null : StrUtil.concat("最小值为", min);
if (field.isAnnotationPresent(DecimalMin.class)) {
if (!field.getAnnotation(DecimalMin.class).inclusive()) {
min = min == null ? null : StrUtil.concat(min, "且不能等于最小值");
}
}
String max = Optional.ofNullable(field.getAnnotation(Max.class)).map((item) -> String.valueOf(item.value())).orElse(null);
if (max == null) {
max = Optional.ofNullable(field.getAnnotation(DecimalMax.class)).map(DecimalMax::value).orElse(null);
}
if (field.isAnnotationPresent(NegativeOrZero.class)) {
if (max == null || new BigDecimal(max).compareTo(BigDecimal.ZERO) > 0) {
max = "非正数";
}
}
if (field.isAnnotationPresent(Negative.class)) {
if (max == null || new BigDecimal(min).compareTo(BigDecimal.ZERO) >= 0) {
max = "负数";
}
}
max = max == null ? null : StrUtil.concat("最大值为", max);
if (field.isAnnotationPresent(DecimalMax.class)) {
if (!field.getAnnotation(DecimalMax.class).inclusive()) {
min = min == null ? null : StrUtil.concat(min, "且不能等于最大值");
}
}
String digit = Optional.ofNullable(field.getAnnotation(Digits.class)).map((item) -> {
String integer = String.valueOf(item.integer());
String fraction = String.valueOf(item.fraction());
return StrUtil.concat("整数位", integer, ",", "小数位", fraction);
}).orElse(null);
if (min == null && max == null && digit == null) {
return null;
}
return StrUtil.join(",", min, max, digit);
}