-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Closed
Milestone
Description
Hello,
I want to add a bunch of attributes to the resulting JSON file so I create my own StdSerializer
like this:
class MarkupAwareStandardSerializer extends StdSerializer<AbstractEntityDefinition> {
private StdSerializer serializer;
public MarkupAwareStandardSerializer(StdSerializer src) {
super(src);
this.serializer = src;
}
@Override
public void serialize(AbstractEntityDefinition bean, JsonGenerator gen, SerializerProvider provider) throws IOException {
ServletRequestAttributes servletRequestAttributes = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes());
if (servletRequestAttributes != null) {
String filterId = servletRequestAttributes.getRequest().getParameter("filter");
if (StringUtils.isNotEmpty(filterId) && filterId.equals("search")) {
List<ItemMarkup> itemMarkups = entityFacade.getEntityPropertyMarkups(bean.getClass());
gen.writeStartObject(bean);
for (ItemMarkup itemMarkup : itemMarkups) {
if (itemMarkup != null && itemMarkup.isPartOfSearchResult()) {
if (StringUtils.isNotEmpty(itemMarkup.getSearchResultExpression())) {
gen.writeStringField(itemMarkup.getName(),
parseExpression(itemMarkup.getSearchResultExpression(), new ExpressionContext(bean)));
} else {
try {
provider.defaultSerializeField(itemMarkup.getName(), PropertyUtils.getProperty(bean, itemMarkup.getName()), gen);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException nsmex) {
LOG.warn(nsmex.getMessage());
}
}
}
}
gen.writeEndObject();
} else {
serializer.unwrappingSerializer(null).serialize(bean, gen, provider);
}
} else {
serializer.unwrappingSerializer(null).serialize(bean, gen, provider);
}
}
@Override
public boolean isUnwrappingSerializer() {
return true;
}
}
Notice I have overriden the isUnwrappingSerializer()
method to return true. But now I get this exception:
java.lang.ClassCastException: com.nemesis.platform.module.restservices.core.jackson.MarkupAwareSerializerModifier$MarkupAwareStandardSerializer cannot be cast to com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanSerializer
at com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter._findAndAddDynamic(UnwrappingBeanPropertyWriter.java:214)
at com.fasterxml.jackson.databind.ser.impl.UnwrappingBeanPropertyWriter.serializeAsField(UnwrappingBeanPropertyWriter.java:102)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.SerializerProvider.defaultSerializeValue(SerializerProvider.java:1033)
at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer.serialize(PersistentEntityJackson2Module.java:202)
at org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer.serialize(PersistentEntityJackson2Module.java:150)
you are trying to cast the serializer to UnwrappingBeanSerializer
which I don't think is correct.
Metadata
Metadata
Assignees
Labels
No labels