java - Custom Serialization based on User defined value -
i've pojo :
@jsonignoreproperties(ignoreunknown = true) public class item { string name; long baseid; @jsonignore string status; }
i'm using java object get, post , put api calls. i've used @jsonignore annotations avoid serializations(so wont serialize put , post request got get request). i'm in need baseid should serialized post request , not put request. please suggest me solutions.
you can consider using the jackson json views. basically, replace @jsonignore
annotation on jackson view annotation view type , enable depending on context. here example plain object mapper:
public class jacksonview { public static interface view1 { } public static interface view2 { } public static class bean { @jsonview(view1.class) public final string field1; @jsonview(view2.class) public final string field2; public final string field3; public bean(string field1, string field2, string field3) { this.field1 = field1; this.field2 = field2; this.field3 = field3; } } public static void main(string[] args) throws jsonprocessingexception { bean bean = new bean("a", "b", "c"); objectmapper mapper = new objectmapper(); system.out.println(mapper .writer() .withview(view2.class) .writevalueasstring(bean)); } }
output:
{"field2":"b","field3":"c"}
to make work in jax-rs should enable specific view annotating http method resources described here. example:
@get @jsonview(view1.class) public bean doget() {...} @post @jsonview(view2.class) public void dopost(bean bean) {...}
Comments
Post a Comment