在grails 2.3中,我想通过JSON发送定制的错误消息。但是当我使用
class ApiError {
String message
String status
Exception error
}并尝试解析它,我得到了:(我省略了无关的消息和状态as JSON )
"error": {
"cause": null
"class": "grails.validation.ValidationException"
"errors": {
"errors": [1]
0: {
"object": "com.example.Firm"
"field": "context"
"rejected-value": null
"message": "Property [context] of class [class com.example.Firm] cannot be null"
}
}
"localizedMessage": "Firm is not valid: - Field error in object 'com.example.Firm' on field 'context': rejected value [null]; codes [com.example.Firm.context.nullable.error.com.example.Firm.context,com.example.Firm.context.nullable.error.context,com.example.Firm.context.nullable.error.java.lang.String,com.example.Firm.context.nullable.error,firm.context.nullable.error.com.example.Firm.context,firm.context.nullable.error.context,firm.context.nullable.error.java.lang.String,firm.context.nullable.error,com.example.Firm.context.nullable.com.example.Firm.context,com.example.Firm.context.nullable.context,com.example.Firm.context.nullable.java.lang.String,com.example.Firm.context.nullable,firm.context.nullable.com.example.Firm.context,firm.context.nullable.context,firm.context.nullable.java.lang.String,firm.context.nullable,nullable.com.example.Firm.context,nullable.context,nullable.java.lang.String,nullable]; arguments [context,class com.example.Firm]; default message [Property [{0}] of class [{1}] cannot be null] "
"message": "firm is not valid: - Field error in object 'com.example.Firm' on field 'context': rejected value [null]; codes [com.example.Firm.context.nullable.error.com.example.Firm.context,com.example.Firm.context.nullable.error.context,com.example.Firm.context.nullable.error.java.lang.String,com.example.Firm.context.nullable.error,firm.context.nullable.error.com.example.Firm.context,firm.context.nullable.error.context,firm.context.nullable.error.java.lang.String,firm.context.nullable.error,com.example.Firm.context.nullable.com.example.Firm.context,com.example.Firm.context.nullable.context,com.example.Firm.context.nullable.java.lang.String,com.example.Firm.context.nullable,firm.context.nullable.com.example.Firm.context,firm.context.nullable.context,firm.context.nullable.java.lang.String,firm.context.nullable,nullable.com.example.Firm.context,nullable.context,nullable.java.lang.String,nullable]; arguments [context,class com.example.Firm]; default message [Property [{0}] of class [{1}] cannot be null] "
"stackTrace": [...143]
"suppressed": [0]
}我不明白;在错误中使用错误数组有什么意义?如何只得到一个错误数组呢?Here是这个类的源代码。
发布于 2016-02-01 00:40:05
Grails验证错误包括全局错误和字段错误。每当grails中发生验证异常时,它不会为每个字段错误抛出新的异常,而是将所有字段错误包装在一个error对象中。可能存在全局对象错误,这是拒绝对象的全局原因。
因此,如果您希望避免错误内部的错误,那么您应该修改您的ApiError类,将Exception error替换为List<Errors> errors,并从instance.errors设置它的值。
或者,您可以使用自定义方法解析错误: CodecLookup codecLookup
protected List retrieveErrors(instance) {
CodecLookup codec = Holders.applicationContext.getBean(CodecLookup)
List errors = []
g.eachError([bean: instance], {
error ->
errors.add(codec.lookupDecoder('HTML').decode(g.message(error: error)))
})
return errors
}g是每个控制器中可用的ValidationTagLib的名称空间。
https://stackoverflow.com/questions/35098129
复制相似问题