下面列出了怎么用org.hibernate.validator.constraints.Length的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Sets max length for textual UI component using Entity metadata.
*
* @param component UI component
* @param valueSource value source
*/
public void setupMaxLength(TextInputField.MaxLengthLimited component, EntityValueSource valueSource) {
MetaProperty metaProperty = valueSource.getMetaPropertyPath().getMetaProperty();
Map<String, Object> annotations = metaProperty.getAnnotations();
Integer maxLength = (Integer) annotations.get("length");
if (maxLength != null) {
component.setMaxLength(maxLength);
}
Integer sizeMax = (Integer) annotations.get(Size.class.getName() + "_max");
if (sizeMax != null) {
component.setMaxLength(sizeMax);
}
Integer lengthMax = (Integer) annotations.get(Length.class.getName() + "_max");
if (lengthMax != null) {
component.setMaxLength(lengthMax);
}
}
private void givenFieldDescriptorsWithConstraints() {
Attribute constraintAttributeWithNotNull = Attributes.key("notImportant").value(singletonList(new Constraint(NotNull.class.getName(), emptyMap())));
HashMap<String, Object> lengthAttributes = new HashMap<>();
lengthAttributes.put("min", 2);
lengthAttributes.put("max", 255);
Attribute constraintAttributeWithLength = Attributes.key("notImportant").value(singletonList(new Constraint(Length.class.getName(), lengthAttributes)));
fieldDescriptors = Arrays.asList(
fieldWithPath("id").description("some").type(STRING).attributes(constraintAttributeWithNotNull),
fieldWithPath("lineItems[*].name").description("some").type(STRING).type(STRING).attributes(constraintAttributeWithLength),
fieldWithPath("lineItems[*]._id").description("some").type(STRING).attributes(constraintAttributeWithNotNull),
fieldWithPath("lineItems[*].quantity.value").description("some").type(NUMBER).attributes(constraintAttributeWithNotNull),
fieldWithPath("lineItems[*].quantity.unit").description("some").type(STRING),
fieldWithPath("shippingAddress").description("some").type(OBJECT),
fieldWithPath("billingAddress").description("some").type(OBJECT).attributes(constraintAttributeWithNotNull),
fieldWithPath("billingAddress.firstName").description("some").type(STRING).attributes(Attributes
.key("notImportant")
.value(singletonList(new Constraint(NotEmpty.class.getName(), emptyMap())))),
fieldWithPath("billingAddress.valid").description("some").type(BOOLEAN),
fieldWithPath("paymentLineItem.lineItemTaxes").description("some").type(ARRAY)
);
}
/**
* 读-名称
*
* @return
*/
@Length(max = 200)
@NotBlank
@MetaProperty(messageKey = "property.fdName")
default String getFdName() {
return (String) getExtendProps().get("fdName");
}
/**
* 读-层级ID
*
* @return
*/
@MetaProperty(showType = ShowType.NONE, messageKey = "property.fdHierarchyId")
@Length(max = 900)
@Column(columnDefinition = "varchar(900)")
default String getFdHierarchyId() {
return (String) getExtendProps().get("fdHierarchyId");
}
public RuleDatabaseBase(Integer userId, Integer brokerId,
@NotBlank String databaseUrl, @NotBlank String username,
@NotBlank String password, @NotBlank String datasourceName,
@Length(max = 256) String optionalParameter,
Boolean systemTag, Integer databaseType) {
this.userId = userId;
this.brokerId = brokerId;
this.databaseUrl = databaseUrl;
this.username = username;
this.password = password;
this.datasourceName = datasourceName;
this.optionalParameter = optionalParameter;
this.systemTag = systemTag;
this.databaseType = databaseType;
}
/**
* ok
* 方法上的@Validated注解,一般用来指定 验证组
*
* @param code
* @return
*/
@GetMapping("/requestParam/get")
@Validated
public String paramGet(@Length(max = 3)
@NotEmpty(message = "不能为空")
@RequestParam(value = "code", required = false) String code) {
return "方法上的@Validated注解,一般用来指定 验证组";
}
/**
* Called when posting the search form on the Zwitscher board.
*
* @param q the query string
* @param viewModel the view model used to render the template
* @return the template to use
*/
@RequestMapping(value = "/search", method = RequestMethod.GET)
public String search(@RequestParam("q") @Length(max = 500) String q,
Model viewModel) {
populateDefault(viewModel);
populateTweets(q, viewModel);
return "index";
}
/**
* Find the matching Zwitscher messages for the given query.
*
* @param q the query, max 500 chars long
* @return the tweets, never NULL
*/
@HystrixCommand(fallbackMethod = "none")
public Collection<Zwitscher> findByQ(final @Length(max = 500) String q) {
log.info("Get Zwitscher message from /tweets using q={}.", q);
Zwitscher[] tweets = restTemplate.getForObject(tweetsRibbonUrl, Zwitscher[].class, q);
return Arrays.asList(tweets);
}
@Length(min=0, max=100, message="用户类型长度必须介于 1 和 100 之间")
@ExcelField(title="用户类型", align=2, sort=80, dictType="sys_user_type")
public String getUserType() {
return userType;
}
@Length(min=1, max=100)
public String getEnname() {
return enname;
}
/**
* 读-EntityName
*
* @return
*/
@Length(max = 100)
@MetaProperty(messageKey = "property.fdEntityName")
default String getFdEntityName() {
return (String) getExtendProps().get("fdEntityName");
}
@Length(min=0, max=100)
public String getCode() {
return code;
}
/**
* 读-fdEntityKey
*
* @return
*/
@Length(max = 200)
@MetaProperty(messageKey = "property.fdEntityKey")
default String getFdEntityKey() {
return (String) getExtendProps().get("fdEntityKey");
}
@Length(min=1, max=1)
public String getDelFlag() {
return delFlag;
}
@Length(min=1, max=100, message="工号长度必须介于 1 和 100 之间")
@ExcelField(title="工号", align=2, sort=45)
public String getNo() {
return no;
}
@Length(min=1, max=100)
public String getName() {
return name;
}
/**
* 普通参数校验
* @param name
* @return
*/
@GetMapping("/name")
public String findStudentByName(@NotBlank(message = "学生名字不能为空")
@Length(min = 2, max = 10, message = "name 长度必须在 {min} - {max} 之间")String name){
return "success";
}
@Length(min=0, max=200)
public String getPermission() {
return permission;
}
@Length(min=0, max=100)
public String getIcon() {
return icon;
}
@Length(min=1, max=100)
public String getType() {
return type;
}
@Length(min=0, max=64, message="项目名称长度不能超过 64 个字符")
public String getProjectName() {
return projectName;
}
@Length(min=0, max=200)
public String getPhone() {
return phone;
}
@Length(min=1, max=100)
public String getName() {
return name;
}
@Length(min=0, max=32, message="域名长度不能超过 32 个字符")
public String getSubdomainHost() {
return subdomainHost;
}
@Length(min=1, max=1)
public String getType() {
return type;
}
@Length(min=0, max=255, message="服务器名称长度不能超过 255 个字符")
public String getServerName() {
return serverName;
}
@NotBlank(message="岗位名称不能为空")
@Length(min=0, max=100, message="岗位名称长度不能超过 100 个字符")
public String getPostName() {
return postName;
}
@Length(min=0, max=200)
public String getEmail() {
return email;
}
@NotBlank(message="公司名称不能为空")
@Length(min=0, max=200, message="公司名称长度不能超过 200 个字符")
public String getCompanyName() {
return companyName;
}
@XmlAttribute
@Length(min=0, max=100)
public String getDescription() {
return description;
}