Json 验证对象列表
嗨,我正在尝试验证一个包含一系列对象的对象。根实例来自JSON客户机文件 我创造了这样的东西Json 验证对象列表,json,validation,grails,Json,Validation,Grails,嗨,我正在尝试验证一个包含一系列对象的对象。根实例来自JSON客户机文件 我创造了这样的东西 @Validateable class Book { String title List authors static hasMany = [authors : Authors] static constraints = { authors(nullable:false,validator: { recipients ->
@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))
}
}
我正试图通过以下方式在控制器中验证它:
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
有人能帮我吗?我找到了解决方案,它在文档中 注册可验证类 如果一个类没有标记为Validateable,那么框架仍然可以使它成为可验证的。执行此操作所需的步骤是在类中定义static constraints属性(如上所述),然后通过在Config.groovy@中为grails.validatable.classes属性赋值来告知框架类:
grails.validateable.classes = [com.mycompany.myapp.User, com.mycompany.dto.Account]
您是否为此url映射配置了
parseRequest:true
?是的,我已经定义了parseRequest:true。我在url映射中看到了这一点:“/rest/$controller/manageBook”(parseRequest:true){action=[POST:“manageBook”]}