I am using an abstract class to localize my models to different languages. This is the inheritance chain that I have set up:
//Base model, contains localized fields
public class Restaurant extends LocalizedModel<LocalizedRestaurantData>{
...
}
//Abstract class to support localized fields for all my models
@XmlRootElement
public abstract class LocalizedModel<T extends LocalizedData> {
private T en;
public T getEn() {
return en;
}
public void setEn(T en) {
this.en = en;
}
...
}
//Implementation of the localized fields for the restaurant class.
@XmlRootElement
public class LocalizedRestaurantData extends LocalizedData{
protected String name;
protected String address;
...
}
This all works fine in my Jersey JSON web service, except for one thing: All the instances of the localized property en contain an extra field type:
Restaurant JSON:
{
"en": {
"type": "localizedRestaurantData",
"address": "1234 Main St.",
"name": "Tacos Folie"
},
...
}
This type field is undesired and undesirable especially since it seems to be also required by Jackson when parsing an object. I’ve added @JsonIgnoreProperties({"type"}) in my code without success.
After multiple attempts and soliciting help on both the Jackson and the Jersey mailing list, the solution I found is:
My JERSEY context was implementing
ContextResolver<JSONJAXBContext>. That needs to be changed toContextResolver<JacksonJsonProvider>to use the pure JSON parser.Secondly, the JacksonJsonProvider needs to configured as follows:
And used as the context.
Finally, the following method needs to be overriden as follows in the ContextResolver: