title: How to return a partial JSON response using Java?
body: <p>I'm building a RESTful API and want to provide developers with the option to choose which fields to return in the JSON response.  <a href="http://blog.apigee.com/detail/restful_api_design_can_your_api_give_developers_just_the_information/" rel="noreferrer">This blog post</a> shows examples of how several API's (Google, Facebook, LinkedIn) allow developers to customize the response. This is referred to as partial response.</p>
  
  <p>An example might look like this:</p>
  
  <pre><code>/users/123?fields=userId,fullname,title
  </code></pre>
  
  <p>In the example above the API should return the userId, fullName and title fields for User "123".</p>
  
  <p>I'm looking for ideas of how to implement this in my RESTful web service.  I'm currently using CXF (edit: and Jackson) but willing to try another JAX-RS implementation.</p>
  
  <p>Here's what I currently have. It returns a full User object.  How can I return only the fields the API caller wants at runtime based on the "fields" paramaeter?  I don't want to make the other fields Null.  I simply don't want to return them.</p>
  
  <pre><code>@GET
  @Path("/{userId}")
  @Produces("application/json")
  public User getUser(@PathParam("userId") Long userId, 
      @DefaultValue("userId,fullname,title") @QueryParam("fields") String fields) {
  
  User user = userService.findOne(userId);
  
  StringTokenizer st = new StringTokenizer(fields, ",");
  while (st.hasMoreTokens()) {
  
      // here's where i would like to select only the fields i want to return
  
  }
  return user;
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I followed unludo's link which then linked to this: <a href="http://wiki.fasterxml.com/JacksonFeatureJsonFilter" rel="noreferrer">http://wiki.fasterxml.com/JacksonFeatureJsonFilter</a></p>
  
  <p>With that info I added <code>@JsonFilter("myFilter")</code> to my domain class. Then I modified my RESTful service method to return String instead of User as follows:</p>
  
  <pre><code>@GET
  @Path("/{userId}")
  @Produces("application/json")
  public String getUser(@PathParam("userId") Long userId,
                      @DefaultValue("userId,fullname,title") @QueryParam("fields") String fields) {
  
      User user = userService.findOne(userId);
  
      StringTokenizer st = new StringTokenizer(fields, ",");
      Set&lt;String&gt; filterProperties = new HashSet&lt;String&gt;();
      while (st.hasMoreTokens()) {
          filterProperties.add(st.nextToken());
      }
  
      ObjectMapper mapper = new ObjectMapper();
      FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter",
                  SimpleBeanPropertyFilter.filterOutAllExcept(filterProperties));
  
      try {
          String json = mapper.filteredWriter(filters).writeValueAsString(user);
          return json;
      } catch (IOException e) {
          e.printStackTrace();
      return e.getMessage();
      }
  }
  </code></pre>
  
  <p>I need to do more testing but so far so good.</p>