gpt4 book ai didi

json - 对象验证 list

转载 作者:行者123 更新时间:2023-12-01 05:28:15 24 4
gpt4 key购买 nike

嗨,我正在尝试使用其中的对象列表来验证对象。根实例来自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/

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