gpt4 book ai didi

java - Pojo验证有效。波科( Kotlin )没有。为什么?

转载 作者:行者123 更新时间:2023-12-02 12:48:19 25 4
gpt4 key购买 nike

我有一个POJO并启用了JSR验证。

出于演示的原因,我已经完全重新实现了Kotlin的整个项目,并且我想演示验证方面。只是,在Kotlin中,它根本不起作用,原因也不明显。有人可以解释我所缺少的吗?

启用验证的Java:

@Configuration
@RequiredArgsConstructor
public static class ValidationConfiguration extends RepositoryRestConfigurerAdapter {

private final Validator jsr303Validator;

@Override
public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
//bean validation always before save and create
validatingListener.addValidator("beforeCreate", jsr303Validator);
validatingListener.addValidator("beforeSave", jsr303Validator);

}


}

POJO:
@Getter
@EqualsAndHashCode
@Entity
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class Employee implements Identifiable<Long>{


@NotNull
@Pattern(regexp = "[A-Za-z0-9]+")
@Size(min = 2, max = 32)
private String name;

@Email
@NotNull
private String email;

@NotNull
@PastOrPresent
private LocalDate hireDate = LocalDate.now();

@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
private List<Form> forms = Collections.emptyList();

@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL)
private List<Report> reports = Collections.emptyList();

@Id
@GeneratedValue
private Long id;

public Employee(String name, String email) {
this.name = name;
this.email = email;
}

public Employee(String name) {
this(name,name +"@sterlingts.com");
}
}

Kotlin验证码:
@Configuration
class ValidationConfiguration(private val jsr303Validator: Validator) : RepositoryRestConfigurerAdapter() {
override fun configureValidatingRepositoryEventListener(validatingListener: ValidatingRepositoryEventListener) {
//bean validation always before save and create
validatingListener.addValidator("beforeCreate", jsr303Validator)
validatingListener.addValidator("beforeSave", jsr303Validator)

}
}

POKO:
@Entity
data class Employee(@Pattern(regexp = "[A-Za-z0-9]+")
@Size(min = 6, max = 32)
val name: String,
@Email
@NotNull
val email: String?,
@PastOrPresent
val hireDate: LocalDate = LocalDate.now(),

@OneToMany(mappedBy = "employee", cascade = [CascadeType.ALL])
val forms:List<Form> = listOf(),
@OneToMany(mappedBy = "employee", cascade = [CascadeType.ALL])
val reports:List<Report> = listOf(),
@Id @GeneratedValue( strategy = GenerationType.IDENTITY) private val id: Long? = null): Identifiable<Long> {

override fun getId() = id

constructor(name:String): this(name,"$name@sterlingts.com")
}

例:

POST /employees


{
"name": "christian",
"email": "This is not an email"
}

结果(预期的Java版本):
{
"errors": [
{
"entity": "Employee",
"property": "email",
"invalidValue": "This is not an email",
"message": "must be a well-formed email address"
}
]
}

现在,作为Kotlin Poko:

POST /employees


{
"name": "christian",
"email": "This is not an email"
}

结果(201创建):
{
"name" : "christian",
"email" : "This is not an email",
"hireDate" : "2018-02-26",
"_links" : {
"self" : {
"href" : "http://localhost:8080/employees/19"
},
"employee" : {
"href" : "http://localhost:8080/employees/19"
},
"forms" : {
"href" : "http://localhost:8080/employees/19/forms"
},
"reports" : {
"href" : "http://localhost:8080/employees/19/reports"
}
}
}

最佳答案

data class A(@Email val email: String)中,注释默认情况下应用于构造函数参数。换句话说,它等效于:

class A{
public A(@Email String email) { ... }
}

在您的情况下,您希望将注释应用于字段,因此您需要编写:
data class A(@field:Email val email: String)

有关 the docs中的注释的更多信息。

关于java - Pojo验证有效。波科( Kotlin )没有。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48993437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com