尝试创建对属性名称或类使用特定格式的自定义编组器。只需查看下面的 marshaller 并对其进行修改:
class CustomDtoObjectMarshaller implements ObjectMarshaller<JSON>{
String[] excludedProperties=['metaClass']
public boolean supports(Object object) {
return object instanceof GroovyObject;
}
public void marshalObject(Object o, JSON json) throws ConverterException {
JSONWriter writer = json.getWriter();
try {
writer.object();
for (PropertyDescriptor property : BeanUtils.getPropertyDescriptors(o.getClass())) {
String name = property.getName();
Method readMethod = property.getReadMethod();
if (readMethod != null && !(name in excludedProperties)) {
Object value = readMethod.invoke(o, (Object[]) null);
if (value!=null) {
writer.key(name);
json.convertAnother(value);
}
}
}
for (Field field : o.getClass().getDeclaredFields()) {
int modifiers = field.getModifiers();
if (Modifier.isPublic(modifiers) && !(Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers))) {
writer.key(field.getName());
json.convertAnother(field.get(o));
}
}
writer.endObject();
}
catch (ConverterException ce) {
throw ce;
}
catch (Exception e) {
throw new ConverterException("Error converting Bean with class " + o.getClass().getName(), e);
}
}
}
引导程序中的寄存器:
CustomDtoObjectMarshaller customDtoObjectMarshaller=new CustomDtoObjectMarshaller()
customDtoObjectMarshaller.excludedProperties=['metaClass','class']
JSON.registerObjectMarshaller(customDtoObjectMarshaller)
在我的示例中,我只是 scip 'metaClass' 和 'class' 字段。我认为最常见的方式