Grails - grails.converters.JSON - 删除类名 [英] Grails - grails.converters.JSON - removing the class name
本文介绍了Grails - grails.converters.JSON - 删除类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法删除 JSON 转换器中的类字段?
Is there a way to remove the class field in a JSON converter?
示例:
import testproject.*
import grails.converters.*
emp = new Employee()
emp.lastName = "Bar"
emp as JSON
作为一个字符串是
{"class":"testproject.Employee","id":null,"lastName":"Bar"}
我更喜欢
{"id":null,"lastName":"Bar"}
有没有办法在最后多加一行代码去掉class字段?
Is there a way to add one more line of code at the end to remove the class field?
推荐答案
这里还有一种方法可以做到.我在域类中添加了下一个代码:
Here is yet one way to do it. I've added a next code to the domain class:
static {
grails.converters.JSON.registerObjectMarshaller(Employee) {
return it.properties.findAll {k,v -> k != 'class'}
}
}
但正如我发现的那样,如果您使用 Groovy @ToString 类注释时还必须添加 'class' 以排除参数,例如:
But as I found if you have used Groovy @ToString class annotation when you also must add 'class' to excludes parameter, e.g.:
@ToString(includeNames = true, includeFields = true, excludes = "metaClass,class")
这篇关于Grails - grails.converters.JSON - 删除类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文