下面列出了怎么用javax.validation.constraints.Positive的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public PropertyModel postProcessInternal(PropertyModel propertyModel) {
Positive positiveAnno = propertyModel.getPropertyItem().getAnnotation(Positive.class);
if (positiveAnno == null) return propertyModel;
propertyModel.setDescription(TextUtils.combine(
propertyModel.getDescription(),
" (值只能为正数,不包括0)"
));
return propertyModel;
}
/**
* Determine a number type's minimum (exclusive) value.
*
* @param member the field or method to check
* @return specified exclusive minimum value (or null)
* @see DecimalMin
* @see Positive
*/
protected BigDecimal resolveNumberExclusiveMinimum(MemberScope<?, ?> member) {
DecimalMin decimalMinAnnotation = this.getAnnotationFromFieldOrGetter(member, DecimalMin.class, DecimalMin::groups);
if (decimalMinAnnotation != null && !decimalMinAnnotation.inclusive()) {
return new BigDecimal(decimalMinAnnotation.value());
}
Positive positiveAnnotation = this.getAnnotationFromFieldOrGetter(member, Positive.class, Positive::groups);
if (positiveAnnotation != null) {
return BigDecimal.ZERO;
}
return null;
}
@GetMapping("/{message}")
public String validateParameters(
@PathVariable("message") @Size(min = 5, max = 10) String message,
@RequestParam("size") @Positive Long size) {
return "Query parameters where valid";
}
@GetMapping("/messages")
public List<String> getMessages(@RequestParam("size") @Positive @Max(6) Integer size) {
return List.of("Hello", "World", "Foo", "Bar", "Duke", "Spring")
.stream()
.limit(size)
.collect(Collectors.toList());
}
@GetMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/commits")
public List<CommitDTO> find(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String namespaceName,
@Valid @PositiveOrZero(message = "page should be positive or 0") @RequestParam(defaultValue = "0") int page,
@Valid @Positive(message = "size should be positive number") @RequestParam(defaultValue = "10") int size) {
if (permissionValidator.shouldHideConfigToCurrentUser(appId, env, namespaceName)) {
return Collections.emptyList();
}
return commitService.find(appId, Env.valueOf(env), clusterName, namespaceName, page, size);
}
@GetMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/releases/all")
public List<ReleaseBO> findAllReleases(@PathVariable String appId,
@PathVariable String env,
@PathVariable String clusterName,
@PathVariable String namespaceName,
@Valid @PositiveOrZero(message = "page should be positive or 0") @RequestParam(defaultValue = "0") int page,
@Valid @Positive(message = "size should be positive number") @RequestParam(defaultValue = "5") int size) {
if (permissionValidator.shouldHideConfigToCurrentUser(appId, env, namespaceName)) {
return Collections.emptyList();
}
return releaseService.findAllReleases(appId, Env.valueOf(env), clusterName, namespaceName, page, size);
}
@GetMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/releases/active")
public List<ReleaseDTO> findActiveReleases(@PathVariable String appId,
@PathVariable String env,
@PathVariable String clusterName,
@PathVariable String namespaceName,
@Valid @PositiveOrZero(message = "page should be positive or 0") @RequestParam(defaultValue = "0") int page,
@Valid @Positive(message = "size should be positive number") @RequestParam(defaultValue = "5") int size) {
if (permissionValidator.shouldHideConfigToCurrentUser(appId, env, namespaceName)) {
return Collections.emptyList();
}
return releaseService.findActiveReleases(appId, Env.valueOf(env), clusterName, namespaceName, page, size);
}
/**
* Sets the number of columns. If {@code null} value is passed, columns count will be determined
* based on the {@code rows} parameter.
*
* @param cols positive integer or {@code null}
*/
@StudioProperty(name = "cols")
@Positive
public void setColumnsCount(Integer cols) {
if (cols != null && cols <= 0) {
throw new GuiDevelopmentException(
"DynamicAttributesPanel element has incorrect value of the 'cols' attribute", this.id);
}
this.cols = cols;
}
/**
* Sets the number of rows. This parameter will only be taken into account if {@code cols == null}.
*
* @param rows positive integer or {@code null}
*/
@StudioProperty(name = "rows")
@Positive
public void setRowsCount(Integer rows) {
if (rows != null && rows <= 0) {
throw new GuiDevelopmentException(
"DynamicAttributesPanel element has incorrect value of the 'rows' attribute", this.id);
}
this.rows = rows;
}
@Positive(groups = Test.class)
public byte getPositiveOnGetterByte() {
return positiveOnGetterByte;
}
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);
}