Define JSON provider by request origin

14 views
Skip to first unread message

kevin.s...@dooapp.com

unread,
Jul 11, 2016, 11:30:00 AM7/11/16
to wisdom-discuss
Hi everybody,

I'm looking for a solution to define the JSON provider depending on the request origin, to serialize a bit differenlty the response for application requests and web front requests.

In fact, the application doesn't need all data to be serialized. Some of data are references. So I successfully customized the json provider to summerise this kind of data.

Exemple:

Project P1 has a user U1 (id 1).
When someone request the project, the json will be:

{
   
"name":"P1",
   
"user" : {
         
"@reference" : "1"
   
}
}


Everything works well since the software know how to deserialize @reference. But for the web front, it's a bit different, since its need to have the entire data serialized.

So:

{
   
"name":"P1",
   
"user" : {
         
"name" : "U1"
         
[...] // all other data of the user
   
}
}


The best approach in my opinion is to define the JSON provider by origin. Is there a way to achieve this with wisdom?

Thank you in advance!

Kevin

Rémi Parain

unread,
Jul 12, 2016, 7:32:57 AM7/12/16
to wisdom-discuss
You can use Jackson Views ( http://wiki.fasterxml.com/JacksonJsonViews )  to decorate class fields.
You can then serialize your objects according to a specific view  with a filter  or an interceptor.

Jackson views supports inheritance and work well in wisdom   Json component 'out-of-box'.

@Requires
Json json;

@Validate
public void start() {

List<UserAccount> allAccounts = ... ;

      // sensistive fields (annotated with AdminView)  should not be serialized
      json.mapper().writerWithView( JacksonView.UserView.class ).writeValueAsString(   allAccounts  );
     
      // sensistive will be serialized 
      json.mapper().writerWithView( JacksonView.AdminView.class ).writeValueAsString(   allAccounts  );

}
Reply all
Reply to author
Forward
0 new messages