java - Jackson's @JsonView annotation does not work -
i annotated class user @jsonview , when returned see fields not contains in view class. here class
@entity @table(name = "users") public class user implements serializable{ /** * */ private static final long serialversionuid = 1l; @id @column(name="id") @generatedvalue(strategy=generationtype.auto) private long userid; @jsonview(view.summary.class) @column(name="email") private string email; @jsonview(view.summary.class) @column(name="user_name") private string firstname; @jsonview(view.summary.class) @column(name="user_last_name") private string lastname; @jsonview(view.summary.class) @column(name="phone") private string phone; @jsonview(view.summary.class) @column(name="origin") private string address; @jsonview(view.summary.class) @column(name="birth_date") private long birthdate; @jsonview(view.summary.class) @column(name="gender") private long gender; @jsonview(view.summary.class) @column(name="about_me") private string aboutme; @jsonview(view.summarywithphoto.class) @onetoone(fetch = fetchtype.eager) @joincolumn(name="photo") private photo avatar; @jsonview(view.summarywithsession.class) @transient private usersession session; //getters , setters
here view class
public class view { public interface summary {} public interface summarywithphoto extends summary {} public interface summarywithsession extends summarywithphoto {} }
so request method @jsonview(view.summarywithphoto.class)
annotation userid field shouldn't. here endpoint code
@jsonview(view.summarywithphoto.class) @requestmapping(method = requestmethod.get) public responseentity<user> getuser(@requestheader(value="access-key") string accesskey, @requestheader(value="secret-key") string secretkey)
i've spend debugging time same issue. results are:
all fields included default if not change behavior (see
beanserializerfactory.processviews
). change default do:objectmapper mapper = new objectmapper(); mapper.disable(mapperfeature.default_view_inclusion);
fields, marked
@jsonview
omitted in result if controller method annotated other@jsonview
(seefilteredbeanpropertywriter.serializeasfield
)
so use-case not change default settings, annotate long userid
@jsonview
, getuser
other (not same) view.
code com\fasterxml\jackson\core\jackson-databind\2.8.4\jackson-databind-2.8.4-sources.jar!\com\fasterxml\jackson\databind\mapperfeature.java
* feature enabled default. */ default_view_inclusion(true)
is contradicted of blog https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring, have @ code closer.
Comments
Post a Comment