title: Custom json return in SpringController
body: <p>How to use different json serialization in different springboot controller methods?
  Lets say I have</p>
  
  <pre><code>class Guy{
  private String name;
  private int age;
  private List&lt;String&gt; hobbies;
  }
  </code></pre>
  
  <p>In some controller methods, I just want to return the name of a Guy as a json.
  In some methods I want to return name and age as json.
  And in some methods I just want to return the list as json.
  Annotating the properties in the POJO doesn't make sense, because each method has different return specifications...</p>
  
  <p>First try is to use @JsonView annotation</p>
  
  <pre><code>class Guy{
     @JsonView(Views.MyTestView.class)
     private String name;
     @JsonView(Views.MyTestView.class)
     private int age;
     private List&lt;String&gt; hobbies;
  }
  </code></pre>
  
  <p>My view is the following:</p>
  
  <pre><code>public class Views {
      public class MyTestView{}
  }
  
  </code></pre>
  
  <p>and in the controller I used: </p>
  
  <pre><code>  ObjectMapper mapper = new ObjectMapper();
    mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
    return mapper.writerWithView(Views.MyTestView.class)
                 .writeValueAsString(guy);
  </code></pre>
  
  <p>Edit: Its now working like this</p>