我希望能够做到这样的事情:
@Email
public List<String> getEmailAddresses()
{
return this.emailAddresses;
}
Run Code Online (Sandbox Code Playgroud)
换句话说,我希望列表中的每个项目都被验证为电子邮件地址.当然,注释这样的集合是不可接受的.
有没有办法做到这一点?
有没有办法在@NotEmpty不编写自定义验证的情况下实现hibernate验证?javax.validation包不包含此批注.只有@NotNull.但它不验证非空值但为空值.所以我想看看替代品@NotEmpty.
用@Pattern?怎么样?
我试图让Kotlin在spring-data-rest项目上使用jsr 303验证.
给出以下数据类声明:
@Entity data class User(
@Id
@GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
var id: Long? = null,
@Size(min=5, max=15)
val name: String
)
Run Code Online (Sandbox Code Playgroud)
@Size注释在这里没有任何效果,使我能够保存名称为1个字符的用户.
它在执行相同的示例时效果很好,但在Java类而不是Kotlin中.
这让我想起了Kotlin问题.
在此先感谢您的帮助!
我正在验证Jersey中REST资源端点中的POJO:
public class Resource {
@POST
public Response post(@NotNull @Valid final POJO pojo) {
...
}
}
public class POJO {
@NotNull
protected final String name;
@NotNull
@Valid
protected final POJOInner inner;
...
}
public class POJOInner {
@Min(0)
protected final int limit;
...
}
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常.
但是,@Min(0)仅当字段inner具有@Valid注释时才会验证注释.将@Valid注释添加到不是基元的每个字段是不正确的.
有没有办法告诉bean验证器自动递归继续验证,即使没有@Valid注释?我希望我POJO如下:
public class POJO {
@NotNull
protected final String name;
@NotNull
protected final POJOInner inner;
...
}
Run Code Online (Sandbox Code Playgroud) 在Spring MVC REST服务(json)中,我有一个像这样的控制器方法:
@RequestMapping(method = RequestMethod.POST, value = { "/doesntmatter" })
@ResponseBody
public List<...> myMethod(@Valid @RequestBody List<MyBean> request, BindingResult bindingResult) {
Run Code Online (Sandbox Code Playgroud)
MyBean类具有bean验证注释的位置.
在这种情况下似乎没有进行验证,尽管它适用于其他控制器.
我不想将列表封装在dto中,这将改变json输入.
为什么没有对bean列表进行验证?有哪些替代方案?
我目前正在使用Spring MVC Web应用程序并尝试使用@Valid注释挂钩验证.当我启动应用程序时,我得到以下异常:
javax.validation.ValidationException: Unable to find a default provider
Run Code Online (Sandbox Code Playgroud)
我在类路径上有Hibernate Validator 3.1.0.GA以及javax验证1.0.0.GA,Hibernate Core 3.3.1.GA和Hibernate Annotations 3.4.0.GA.
在那些我没有看到的版本中是否存在不兼容性,或者是否有人会想到为什么我仍然在类路径上使用Hibernate Validator获得此异常的原因?
干杯,
帽子
我必须更新数据库中的信息.
FacadePatient.java 班级代码:
public Patient update(Patient p) {
Patient pat = em.find(Patient.class, p.getPatientId());
p.setPatientPhone(pat.getPatientPhone());
p.setPatientDateNaiss(pat.getPatientDateNaiss());
p.setPatientEmail(pat.getPatientEmail());
p.setPatientJob(pat.getPatientJob());
p.setPatientSmoking(pat.getPatientSmoking());
p.setPatientSize(pat.getPatientSize());
em.merge(pat);
return p;
}
Run Code Online (Sandbox Code Playgroud) 我想自定义弹簧验证错误
@NotNull
@Length(max = 80)
private String email;
Run Code Online (Sandbox Code Playgroud)
但我无法做到.要遵循的步骤是什么?
我不明白为什么JSR 303(bean验证)是针对getter方法而不是setter的?将它放在setter方法下是不是更合乎逻辑,因为那是进入字段的入口点,应该在此之前检查验证?
我有一个spring boot项目,它有一个CrudRepository,一个Entity和一个Controller.我基本上试图根据传递给Controller的数据来持久化实体.
要做到这一点,我正在使用spring-boot-starter-jpa.我的实体使用JSR-303注释进行注释,在将数据传递给CrudRepository以进行持久化之前,在控制器中对其进行检查.
控制器方法:
@RequestMapping(value = "users", method = { RequestMethod.POST })
public SuccessfulResponse<User> addUser(@Valid @RequestBody User user, BindingResult validation) {
if (validation.hasErrors()) {
throw new ValidationException(validation);
}
User saved = this.users.save(user);
return new SuccessfulResponse<User>(saved);
}
Run Code Online (Sandbox Code Playgroud)
实体:
@Entity /* JPA */
public class User {
@Id /* JPA */
@Column(name="email_address", nullable=false, length=255) /* JPA */
@UserUnique
private String emailAddress;
}
Run Code Online (Sandbox Code Playgroud)
我的问题的原因是UserUnique注释.它的验证器如下所示:
public class UserUniqueValidator implements ConstraintValidator<UserUnique, String> {
private UserRepository users;
@Autowired …Run Code Online (Sandbox Code Playgroud) bean-validation ×10
java ×6
spring ×3
jpa ×2
validation ×2
annotations ×1
collections ×1
data-class ×1
getter ×1
hibernate ×1
java-ee ×1
jax-rs ×1
jersey ×1
kotlin ×1
notnull ×1
rest ×1
spring-boot ×1
spring-mvc ×1