Reputation: 242
I need my Grails app to communicate (JSON) with client that uses Jackson. By default Grails include 'class' in the generated json. On the other hand, default Jackson's ObjectMapper
doesn't recognize 'class', it throws error.
I've found solution, by adding @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="class")
on each POJO classes that are being used.
But, I don't prefer that approach. Because now my POJO will have dependency to Jackson's jar. Is there another easy alternative?
I was trying to figure out from objectMapper.getSerializationConfig(). ...
, but still couldn't found the alternative to replace that annotation.
Thanks
Upvotes: 2
Views: 2826
Reputation: 2824
Are you using the type info for polymorphic instantiation or are you ignoring that information? You could probably use instead @JsonIgnoreProperties({"class"})
on your classes.
If you really don't want to add annotation to your classes, you can use the mix-in mechanism. Although it is much more easier to add the annotation directly to your classes, if you control the classes of course.
mapper.addMixInAnnotations(YourClass.class, TypeInfoMixIn.class);
mapper.addMixInAnnotations(YourSecondClass.class, TypeInfoMixIn.class);
// etc.
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="class")
public class TypeInfoMixIn {}
Upvotes: 2