@JsonSerialize(using = ItemRowSerializer.class )
private ItemRow tes
tItem(){
return new ItemRow("abc", Arrays.asList("item1", "item2", "item3"));
}
I test it out like this
String jsonResult = new ObjectMapper().writeValueAsString(testItem());
System.out.println(jsonResult);
SimpleModule module = new SimpleModule()
module.addSerializer(ItemRowSerializer.class)
mapper.register(module);
String jsonResult = new ObjectMapper().writeValueAsString(testItem());
System.out.println(jsonResult);
@JsonSerializable(using = ItemRowSerailizer
public class ItemRow<T> {...}
Since I am passing this serialized object over the wire, I do not prefer to write to mapper and send string representation across.
how would I go about getting method level annotation working without module registration?
for details on the actual serializer implementation, please refer here
public class Wrapper {
private Map<RecordDomain, ItemRow> foo;
public Wrapper(Map<RecordDomain, ItemRow> fooz) {
foo = new HashMap<>(fooz);
}
@JsonSerialize( contentUsing = ItemRowSerializer.class)
public Map<RecordDomain, ItemRow> getFoo() {
return foo;
}
public void setFoo(Map<RecordDomain, ItemRow> foo) {
this.foo = foo;
}
}
public class Wrapper {
@JsonSerialize(contentUsing = ItemRowSerializer.class)
private Map<RecordDomain, ItemRow> foo;
public Wrapper(Map<RecordDomain, ItemRow> fooz) {
foo = new HashMap<>(fooz);
}
public Map<RecordDomain, ItemRow> getFoo() {
@JsonAnyGetter
@JsonSerialize(contentUsing = ItemRowSerializer.class)
private Map<RecordDomain, ItemRow> foo;
I just added @JsonAnyGetter to the wrapper class and now when I write mapper.writeValueAsString(wrapper), it gives the expected output. I don't understand why but it just works!
@JsonAnyGetter
@JsonSerialize(contentUsing = ItemRowSerializer.class)
private Map<RecordDomain, ItemRow> foo;