I am getting this exception when I try to convert my object to JSON using Jackson.
I have a pretty simple interface
public interface Spreadsheet {
ExcelSheet getSheetName();
}
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.WRAPPER_OBJECT, property="type")
@JsonSubTypes({
@JsonSubTypes.Type(value=Civ.class, name="civ"),
@JsonSubTypes.Type(value=GreatPerson.class, name="greatperson"),
@JsonSubTypes.Type(value=Wonder.class, name="wonder")
})
public interface Item<T> extends Spreadsheet {
//some methods
}
@JsonTypeName("civ")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "objectType")
public class Civ implements Item<Civ> {
//Implementation
}
Then a class using Items
public class PBF {
private String id;
private List<Player> players = Lists.newArrayList();
private List<Item> items = Lists.newArrayList();
}
When I try to create a JSON using this code, I get the exception defined in the title
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(pbf);
The entire source code is on Github. If you want to reproduce, just clone the repo, and run `mvn clean install && mvn exec:java then point your browser to localhost:8080/games
PS: Required Java 8 and MongoDB
I managed to fix it.
I had to remove the from the interface.
So now it looks like this
public interface Item extends SpreadSheet {
}
public class Civ implements Item {
}
The problem was that I had the same object in for instance Civ class in the type.
I don't think I need the type anyways, so I am good
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments