title: How to dynamically remove fields from a json response?
body: <p>I'm using spring to create a restful api, so far so good, my question is how to model the fields that go to the response dynamically?</p>
  
  <p>Thats the controller that i'm using:</p>
  
  <pre><code>@Controller
  
  public class AlbumController {
  
  @Autowired
  private MusicService musicService;
  
  @RequestMapping(value = "/albums", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public Collection&lt;Resource&lt;Album&gt;&gt; getAllAlbums() {
  
      Collection&lt;Album&gt; albums = musicService.getAllAlbums();
      List&lt;Resource&lt;Album&gt;&gt; resources = new ArrayList&lt;Resource&lt;Album&gt;&gt;();
      for (Album album : albums) {
          resources.add(this.getAlbumResource(album));
      }
      return resources;
  
  }
  
  @RequestMapping(value = "/album/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public Resource&lt;Album&gt; getAlbum(@PathVariable(value = "id") String id) {
  
      Album album = musicService.getAlbum(id);
      return getAlbumResource(album);
  
  }
  
  private Resource&lt;Album&gt; getAlbumResource(Album album) {
  
      Resource&lt;Album&gt; resource = new Resource&lt;Album&gt;(album);
  
  
      // Link to Album
      resource.add(linkTo(methodOn(AlbumController.class).getAlbum(album.getId())).withSelfRel());
      // Link to Artist
      resource.add(linkTo(methodOn(ArtistController.class).getArtist(album.getArtist().getId())).withRel("artist"));
      // Option to purchase Album
      if (album.getStockLevel() &gt; 0) {
          resource.add(linkTo(methodOn(AlbumController.class).purchaseAlbum(album.getId())).withRel("album.purchase"));
      }
  
      return resource;
  
  }
  
  @RequestMapping(value = "/album/purchase/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  @ResponseBody
  public Resource&lt;Album&gt; purchaseAlbum(@PathVariable(value = "id") String id) {
  
      Album a = musicService.getAlbum(id);
      a.setStockLevel(a.getStockLevel() - 1);
      Resource&lt;Album&gt; resource = new Resource&lt;Album&gt;(a);
      resource.add(linkTo(methodOn(AlbumController.class).getAlbum(id)).withSelfRel());
      return resource;
  
  }
  }
  </code></pre>
  
  <p>And the model:</p>
  
  <pre><code>public class Album {
  
  private final String id;
  private final String title;
  private final Artist artist;
  private int stockLevel;
  
  public Album(final String id, final String title, final Artist artist, int stockLevel) {
      this.id = id;
      this.title = title;
      this.artist = artist;
      this.stockLevel = stockLevel;
  }
  
  public String getId() {
      return id;
  }
  
  public String getTitle() {
      return title;
  }
  
  public Artist getArtist() {
      return artist;
  }
  
  public int getStockLevel() {
      return stockLevel;
  }
  
  public void setStockLevel(int stockLevel) {
      this.stockLevel = stockLevel;
  }
  }
  </code></pre>