Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring and JacksonJson, serialising different fields with views
    text
    copied!<p>In a <a href="https://stackoverflow.com/questions/7578081/spring-and-jackson-json-serialising-two-different-sets-of-fields">previous similar question</a>, I asked about, how to serialise two different sets of fields using JacksonJson and Spring.</p> <p>My use case is the typical Controller mapping with <code>@ResponseBody</code> annotation returning directly a particular object or collections of objects, that are then rendered with JacksonJson whenever the client adds <code>application/json</code> in the <code>Accept</code> header.</p> <p>I had two answers, the first one suggests to return different interfaces with a different getter list, the second suggests to use Json Views.</p> <p>I don't have problems to understand the first way, however, for the second, after reading the documentation on <a href="http://wiki.fasterxml.com/JacksonJsonViews" rel="nofollow noreferrer"><code>JacksonJsonViews</code></a>, I don't know how to implement it with Spring.</p> <p>To stay with the example, I would declare three stub classes, inside the class Views:</p> <pre><code>// View definitions: public class Views { public static class Public { } public static class ExtendedPublic extends PublicView { } public static class Internal extends ExtendedPublicView { } } </code></pre> <p>Then I've to declare the classes mentioned:</p> <pre><code>public class PublicView { } public class ExtendedPublicView { } </code></pre> <p>Why on earth they declare empty static classes and external empty classes, I don't know. I understand that they need a "label", but then the static members of Views would be enough. And it's not that <code>ExtendedPublic</code> extends <code>Public</code>, as it would be logical, but they are in fact totally unrelated.</p> <p>And finally the bean will specify with annotation the view or list of views:</p> <pre><code>//changed other classes to String for simplicity and fixed typo //in classname, the values are hardcoded, just for testing public class Bean { // Name is public @JsonView(Views.Public.class) String name = "just testing"; // Address semi-public @JsonView(Views.ExtendedPublic.class) String address = "address"; // SSN only for internal usage @JsonView(Views.Internal.class) String ssn = "32342342"; } </code></pre> <p>Finally in the Spring Controller, I've to think how to change the original mapping of my test bean:</p> <pre><code>@RequestMapping(value = "/bean") @ResponseBody public final Bean getBean() { return new Bean(); } </code></pre> <p>It says to call:</p> <pre><code>//or, starting with 1.5, more convenient (ObjectWriter is reusable too) objectMapper.viewWriter(ViewsPublic.class).writeValue(out, beanInstance); </code></pre> <p>So I have an <code>ObjectMapper</code> instance coming out of nowhere and an <code>out</code> which is not the servlet typical <code>PrintWriter out = response.getWriter();</code>, but is an instance of <code>JsonGenerator</code> and that can't be obtained with the new operator. So I don't know how to modify the method, here is an incomplete try:</p> <pre><code>@RequestMapping(value = "/bean") @ResponseBody public final Bean getBean() throws JsonGenerationException, JsonMappingException, IOException { ObjectMapper objectMapper = new ObjectMapper(); JsonGenerator out; //how to create? objectMapper.viewWriter(Views.Public.class).writeValue(out, new Bean()); return ??; //what should I return? } </code></pre> <p>So I would like to know if anybody had success using <code>JsonView</code> with Spring and how he/she did. The whole concept seems interesting, but the documentation seems lacking, also the example code is missing.</p> <p>If it's not possible I will just use interfaces extending each others. Sorry for the long question.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload