title: How to use Spring MVC @JsonView when returning an object hierarchy from a Rest Controller
body: <p>I'm building an application which uses Spring MVC 4.10 with jackson 2.3.2.
  I have a Project class which has children Proposal objects and a Customer object. These Proposal objects are complex and I want to return a summarized JSON view of them. A similar situation happens with the Customer object. I'm trying to implement this with @JsonView annotations.</p>
  
  <p>I wanted to ask if extending the views of the member object classes in the container object class view is the way to do this or, if not, if there is a cleaner way to implement this that I am unaware of.</p>
  
  <h2>Context</h2>
  
  <p>Before today, I was under the false impression that you could annotate your controller with multiple views and that the resulting JSON representation would be filtered accordingly.</p>
  
  <pre><code>@JsonView({Project.Extended.class, Proposal.Summary.class, Customer.Summary.class})
  @Transactional
  @RequestMapping(value="/project", method=RequestMethod.GET)
  public @ResponseBody List&lt;Project&gt; findAll() {
      return projectDAO.findAll();    
  }
  </code></pre>
  
  <p>Where each class had its own JsonView annotations and interfaces
  e.g.:</p>
  
  <pre><code>public class Customer {
      ...
      public interface Summary {}
      public interface Normal extends Summary {}
      public interface Extended extends Normal {}
  }
  </code></pre>
  
  <p>Nevertheless, it is only the first view in the array that gets taken into account. According to <a href="https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring">https://spring.io/blog/2014/12/02/latest-jackson-integration-improvements-in-spring</a></p>
  
  <blockquote>
    <p>Only one class or interface can be specified with the @JsonView
    annotation, but you can use inheritance to represent JSON View
    hierarchies (if a field is part of a JSON View, it will be also part
    of parent view). For example, this handler method will serialize
    fields annotated with @JsonView(View.Summary.class) and
    @JsonView(View.SummaryWithRecipients.class):</p>
  </blockquote>
  
  <p>and the official documentation in <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-jsonview">http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-jsonview</a></p>
  
  <blockquote>
    <p>To use it with an @ResponseBody controller method or controller
    methods that return ResponseEntity, simply add the @JsonView
    annotation with a class argument specifying the view class or
    interface to be used:</p>
  </blockquote>
  
  <p>So, I ended up extending the views of the members in the view of the container object, like this</p>
  
  <pre><code>@Entity
  public class Project {
      ...
      public static interface Extended extends Normal, Proposal.Extended {}
      public static interface Normal extends Summary, Customer.Normal {}
      public static interface Summary {}
  }
  </code></pre>
  
  <p>and changed my controller to this</p>
  
  <pre><code>@JsonView(Project.Extended.class)
  @Transactional
  @RequestMapping(value="/project", method=RequestMethod.GET)
  public @ResponseBody List&lt;Project&gt; findAll() {
      return projectDAO.findAll();    
  }
  </code></pre>
  
  <p>This seems to do the trick, but I couldn't find documentation or discussion about this situation. Is this the intended use of JsonViews or is it kind of hackish?</p>
  
  <p>Thank you in advance</p>
  
  <p>-Patricio Marrone</p>