json - 对象验证 list

标签 json validation grails

嗨,我正在尝试使用其中的对象列表来验证对象。根实例来自JSON客户端文件。

我已经创建了这样的东西

@Validateable
class Book {
    String title
    List authors
    static hasMany = [authors : Authors]

    static constraints = {
        authors(nullable:false,validator: { recipients ->
            recipients.every { it.validate() }
        } )     
    }
}

@Validateable
class Author {
    String name
    Integer property

        static constraints = {
        name(nullable:false)    
        property((nullable:false, blank: false))    
    }
}

我正在尝试以这种方式在 Controller 中对其进行验证:
class BookController {

def manageBook(Book book){
    if (book.validate()) {
        //Do my stuff
    }else{
        // return error
    }
}
}

但这根本不起作用,我在控制台中看到此错误:
| Error 2012-05-25 11:26:34,014 [http-bio-8080-exec-1] ERROR errors.GrailsExceptionResolver  - MissingMethodException occurred when processing request: [POST] /BookApp/rest/bookApp/manageBook
No signature of method: org.codehaus.groovy.grails.web.json.JSONObject.validate() is applicable for argument types: () values: []
Possible solutions: wait(), values(). Stacktrace follows:
Message: No signature of method: org.codehaus.groovy.grails.web.json.JSONObject.validate() is applicable for argument types: () values: []
Possible solutions: wait(), values()
   Line | Method
->>  17 | doCall  in dataskaterserver.DeviceSeenWifiData$__clinit__closure1_closure2_closure3
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|   886 | runTask in java.util.concurrent.ThreadPoolExecutor$Worker
|   908 | run . . in     ''
^   680 | run     in java.lang.Thread

有人可以帮我吗..?

最佳答案

我找到了解决方案,它在文档中... http://grails.org/doc/latest/guide/validation.html#validationNonDomainAndCommandObjectClasses

注册可验证的类
如果某个类未标记有Validateable,则框架仍可以使该类可验证。为此,所需的步骤是在类中定义静态约束属性(如上所述),然后通过为Config.groovy @中的grails.validateable.classes属性赋值来告知框架有关类的信息:

grails.validateable.classes = [com.mycompany.myapp.User, com.mycompany.dto.Account]

关于json - 对象验证 list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10752912/

相关文章:

grails - Grails:对关系中的集合进行分页和排序

jquery - MVC3 复杂 JSON 列表绑定(bind)

java - Jackson 默认使用自定义 JSON 反序列化器

django - Django 对象的深度 JSON 序列化

MySQL 从 JSON 对象数组中选择字段

即使元素被禁用,jQuery 验证插件也会失败元素验证

Angular 2+ 自定义验证器 "required if"另一个字段已填写

validation - Excel:验证、保护和 worksheet_beforeDoubleClick 之间存在冲突

session - Grails:如何存储/加载多个 session 变量?

json - 带有JSON参数的Grails域构造函数不起作用