Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add dynamic fields to Spring JSON view response

Tags:

There is a Spring approach to filter out fields from a service response with JSON views, but i am missing an equivalent approach to enrich a response with some dynamic/syntetic fields like this;

class User{
    getFirstName(){...}
    getLastName(){...}
    getCreateDate(){...}
}

class UserViewA{
    getFullName(){
      return getLastName()+", "+getFirstName()
    }
    getCreateDate(){...}
}

class UserViewB{
    getFullName(){
      return getFirstName()+" "+getLastName()
    }
    getCreateDate(){...}
}

I could wrap the user within the view, but I do not want to propagate all needed user fields manually.

My other idea was to extend the views with the user object and create some sort of an reference linker to copy values references from the user object to the view, but this will get complicated with collections.

Is there some other approach or framework to achieve this? Is this concept not addressed in any way at all?

Update:

Clarification by example:

  • I do not want to wrap the User object because I do not want to maintain same getter methods from User class in different UserView objects.
  • I can not extend the User because itsis a domain object loaded from other resource.
  • There should be no reference to different UserView objects in the User object.

I am looking for a kind of facade solution/framework/approach.