我有以下控制器方法:
@RequestMapping(value="/map/update", method=RequestMethod.POST, produces = "application/json; charset=utf-8")
@ResponseBody
public ResponseEntityWrapper updateMapTheme(
HttpServletRequest request,
@RequestBody @Valid List<CompanyTag> categories,
HttpServletResponse response
) throws ResourceNotFoundException, AuthorizationException {
...
}
Run Code Online (Sandbox Code Playgroud)
CompanyTag以这种方式定义:
public class CompanyTag {
@StringUUIDValidation String key;
String value;
String color;
String icon;
Icon iconObj;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
...
}
Run Code Online (Sandbox Code Playgroud)
问题是未触发验证,未验证CompanyTag列表,从不调用"StringUUIDValidation"验证器.
如果我删除了List并且只尝试发送一个CompanyTag,即代替:
@RequestBody @Valid List<CompanyTag> categories,
Run Code Online (Sandbox Code Playgroud)
使用:
@RequestBody @Valid CompanyTag category,
Run Code Online (Sandbox Code Playgroud)
它按预期工作,所以显然Spring不喜欢验证事物列表(尝试使用数组,但也没有用).
任何人都知道缺少什么?
我想存储birthdate所以我选择date了MySQL,当我在我的数据库中创建我的实体时,结果如下:
import java.util.Date;
// ..code
@NotNull(message="fill you birthdate")
@Temporal(TemporalType.DATE)
private Date birthdate;
Run Code Online (Sandbox Code Playgroud)
但是当我试图坚持它时,它给了我这个错误:
在回调事件上执行自动Bean验证时违反了Bean验证约束:'prePersist'.有关详细信息,请参阅嵌入式ConstraintViolations.
我在这做错了什么?我正在阅读有关谷歌定义时区的内容,我来自巴西,我应该怎么做?
编辑
package entity;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import java.util.Date;
import java.util.List;
/**
* The persistent class for the user database table.
*
*/
@Entity
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Temporal(TemporalType.DATE)
private Date birthdate;
@NotNull(message="informe seu e-mail")
@Email(message="e-mail inválido")
private String email;
@NotNull(message="informe …Run Code Online (Sandbox Code Playgroud) 我安装了最后一个JDK 8(b116),但我注意到我不能使用类型注释.例如,如果我写的话,阅读Java教程:
String str = null;
String myString = (@NonNull String) str;
Run Code Online (Sandbox Code Playgroud)
要么
TEST st = new @Interned TEST();
Run Code Online (Sandbox Code Playgroud)
编译器给我以下错误:
annotation type not applicable to this kind of declaration
Run Code Online (Sandbox Code Playgroud)
现在它有效.在使用类型注释之前,我们必须使用注释来注释注释@Target(ElementType.TYPE_USE).看看下面的评论!
我也不明白是否会在JDK中插入注释,如等NonNull,Interned或者我们是否必须下载Checker Framework
是否可以基于一个或多个委托验证规则验证集合的每个元素?例如:
@EachElement({@Min(1), @Max(12)})
private Set<Integer> monthNumbers;
Run Code Online (Sandbox Code Playgroud) 我是新手使用Java Bean验证(JSR-303/JSR-349/Hibernate Validator),并了解一般概念.但是,我不确定如何验证组合类型的内容与类型本身.
例如:
@NotNull
private List<String> myString;
Run Code Online (Sandbox Code Playgroud)
将验证List myString不为null,但不验证列表本身的内容.或者给出其他类型的验证器(Min/Max/etc),如何验证List的各个元素?是否有任何组合类型的通用解决方案?
我有一个这样的整数列表:
private List<Integer> indexes;
Run Code Online (Sandbox Code Playgroud)
有没有办法让有效的个人成员在 0-9 的范围内?我看到@Range 和@Valid,但找不到使其与List 一起使用的方法。
谢谢你的帮助,
我们如何确保列表中的各个字符串不为空/空或遵循特定模式
@NotNull
List<String> emailIds;
Run Code Online (Sandbox Code Playgroud)
我还想添加一个模式
@Pattern("\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b.")
但我可以没有它.但我肯定希望有一个约束,它将检查列表中的任何字符串是否为空或空白.Json架构也将如何
"ids": {
"description": "The ids associated with this.",
"type": "array",
"minItems": 1,
"items": {
"type": "string",
"required" :true }
}
"required" :true does not seem to do the job
Run Code Online (Sandbox Code Playgroud) java ×5
validation ×4
annotations ×1
collections ×1
hibernate ×1
jpa ×1
jpa-2.0 ×1
json ×1
jsr ×1
list ×1
spring ×1