创建一个 Validator

Validator是Cloudopt Next的校验组件,主要用于RESTful注解上,与Intercept使用方法类似。

  1. class TestValidator : Validator {
  2. override suspend fun validate(resource: Resource): Boolean {
  3. println("via ${this.javaClass.simpleName}")
  4. return true
  5. }
  6. override suspend fun error(resource: Resource) {
  7. resource.response.statusCode = 400
  8. resource.renderText("error")
  9. }
  10. }

使用 Validator

只需要在路由方法上声明 @Validator 即可。

  1. @GET("/validator")
  2. @Validator([TestValidator::class, TestValidator2::class])
  3. fun testValidator() {
  4. renderText("success")
  5. }