java - Is it possible to specify a default View for all Jersey resource methods? -


here's situation.

i've large codebase using jersey 2.17, jackson 2.6.3, , spring 4.2.0.

i have quite few models small subset of properties exposed @jsonproperty. rest excluded default.

there many jersey resource methods are not using jsonview annotation.

enter me. need write new internal api endpoints expose different subset of internal/confidential model properties. i've annotated these "private" properties @jsonproperty , @jsonview(supersecretandinternal.class).

the original, public, properties i've added @jsonview(publicforeveryone.class) annotation.

the problem there many many resource method handlers don't use specify jsonview. result properties (publicforeveryone , supersecretandintenal alike) being exposed.


edit. adding example code.

// pojo - before @jsonautodetect(gettervisibility = none, isgettervisibility = none, fieldvisibility = none) class person {      @jsonproperty     string name;     @jsonproperty     string age;      date createddate;     string socialsecuritynumber;      // getters/setters not shown } 

this used in jersey method handler in publicapiresource

@get @path("/people") public person getuser(@pathparam("name") string name) { // user name, return user } 

and result in json:

{ name: "jane", age: 20 } 

now, come along , want expose sensitive fields of pojo in alternate , private/internal api. modify pojo so:

// pojo - after @jsonautodetect(gettervisibility = none, isgettervisibility = none, fieldvisibility = none) class person {      @jsonproperty     @jsonview(publicforeveryone.class)     string name;      @jsonproperty     @jsonview(publicforeveryone.class)     string age;      @jsonproperty     @jsonview(supersecretandinternal.class)     date createddate;      @jsonproperty     @jsonview(supersecretandinternal.class)     string socialsecuritynumber;      // getters/setters not shown } 

while creating views:

public class views {      public static class publicforeveryone     {}     public static class supersecretandinternal extends publicforeveryone     {} } 

and create a new jersey resource handler in privateapiresource

@get @path("/internal/secret/people") @jsonview( supersecretandinternal.class ) public person getuser(@pathparam("name") string name) { // user name, return user } 

which returns json:

{ name: "jane", age: 20, createddate: xxx, socialsecuritynumber: 123  } 

but original publicapiresource returns entire person pojo because not have @jsonview annotation. in simple example add annotation , done, in real codebase, there many resources, not mention instances people pojo nested field in other models.


question: there way specify default jsonview per pojo/model? intent such if jersey resource method handler, not specify @jsonview, default 1 used, instead of exposing every @jsonproperty annotated property.

if not, know way can achieve this, without modifying resource method handlers (because can't)?

could provide default serializer pojo serializers "default" view, somehow allows explicit views pass through?

what can configure default inclusion/exclusion, think not solve problem. have check jsonfilter http://wiki.fasterxml.com/jacksonfeaturejsonfilter, has feature defined default filter can applied pojos/models.

example pojo:

class person{     string name;     string age;     date createddate; } class user{     string username;     string password;     date createddate; } 

customannotationinspector:

public class customintrospector extends jacksonannotationintrospector {   @override   public object findfilterid(annotatedclass ac) {     // let's default current behavior if annotation found:     object id = super.findfilterid(ac);     // use simple class name if not     if (id == null) {        id = "default_filter";     }     return id; } 

plug obejctmapper:

objectmapper mapper = new objectmapper();  // create filter id "default_filter" , configure exclude 'createddate' property. filterprovider filters = new simplefilterprovider().addfilter("default_filter", simplebeanpropertyfilter.serializeallexcept("createddate"));  jacksonannotationintrospector jai = new customintrospector ();  // plug custom annotation use "default_filter" if pojo doesn't have @jsonfilter annotation. string jsonperson = mapper.setannotationintrospector(jai).writer(filters).writevalueasstring(new person()); string jsonuser = mapper.setannotationintrospector(jai).writer(filters).writevalueasstring(new user()); 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -