title: Spring Boot: Wrapping JSON response in dynamic parent objects
body: <p>I have a REST API specification that talks with back-end microservices, which return the following values:</p>
  
  <p>On "collections" responses (e.g. GET /users) :</p>
  
  <pre><code>{
      users: [
          {
              ... // single user object data
          }
      ],
      links: [
          {
              ... // single HATEOAS link object
          }
      ]
  }
  </code></pre>
  
  <p>On "single object" responses (e.g. <code>GET /users/{userUuid}</code>) :</p>
  
  <pre><code>{
      user: {
          ... // {userUuid} user object}
      }
  }
  </code></pre>
  
  <p>This approach was chosen so that single responses would be extensible (for example, maybe if <code>GET /users/{userUuid}</code> gets an additional query parameter down the line such at <code>?detailedView=true</code> we would have additional request information).</p>
  
  <p>Fundamentally, I think it is an OK approach for minimizing breaking changes between API updates. However, translating this model to code is proving very arduous.</p>
  
  <p>Let's say that for single responses, I have the following API model object for a single user:</p>
  
  <pre><code>public class SingleUserResource {
      private MicroserviceUserModel user;
  
      public SingleUserResource(MicroserviceUserModel user) {
          this.user = user;
      }
  
      public String getName() {
          return user.getName();
      }
  
      // other getters for fields we wish to expose
  }
  </code></pre>
  
  <p>The advantage of this method is that we can expose <em>only</em> the fields from the internally used models for which we have public getters, but not others. Then, for collections responses I would have the following wrapper class:</p>
  
  <pre><code>public class UsersResource extends ResourceSupport {
  
      @JsonProperty("users")
      public final List&lt;SingleUserResource&gt; users;
  
      public UsersResource(List&lt;MicroserviceUserModel&gt; users) {
          // add each user as a SingleUserResource
      }
  }
  </code></pre>
  
  <p>For <em>single object</em> responses, we would have the following:</p>
  
  <pre><code>public class UserResource {
  
      @JsonProperty("user")
      public final SingleUserResource user;
  
      public UserResource(SingleUserResource user) {
          this.user = user;
      }
  }
  </code></pre>
  
  <p>This yields <code>JSON</code> responses which are formatted as per the API specification at the top of this post. The upside of this approach is that we only expose those fields that we <em>want</em> to expose. The heavy downside is that I have a <em>ton</em> of wrapper classes flying around that perform no discernible logical task aside from being read by Jackson to yield a correctly formatted response.</p>
  
  <p>My questions are the following:</p>
  
  <ul>
  <li><p>How can I possibly generalize this approach? Ideally, I would like to have a single <code>BaseSingularResponse</code> class (and maybe a <code>BaseCollectionsResponse extends ResourceSupport</code> class) that all my models can extend, but seeing how Jackson seems to derive the JSON keys from the object definitions, I would have to user something like <code>Javaassist</code> to add fields to the base response classes at Runtime - a dirty hack that I would like to stay as far away from as humanly possible.</p></li>
  <li><p>Is there an easier way to accomplish this? Unfortunately, I may have a variable number of top-level JSON objects in the response a year from now, so I cannot use something like Jackson's <code>SerializationConfig.Feature.WRAP_ROOT_VALUE</code> because that wraps <em>everything</em> into a single root-level object (as far as I am aware).</p></li>
  <li><p>Is there perhaps something like <code>@JsonProperty</code> for class-level (as opposed to just method and field level)?</p></li>
  </ul>