繁体   English   中英

Pojo 验证有效。 Poko (kotlin) 没有。 为什么?

[英]Pojo validation works. Poko (kotlin) doesn't. Why?

我有一个 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)

    }
}

博科:

@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 /员工

{
 "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 /员工

{
 "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)

更多关于文档中的注释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM