Note that there are some explanatory texts on larger screens.

plurals
  1. POJackson JSON Views ignored
    text
    copied!<p>I am trying to use Jackson2.0.0 with Spring3.1 so that I can use the jackson-Module-Hibernate. I have followed the steps as described here <a href="http://blog.pastelstudios.com/2012/03/12/spring-3-1-hibernate-4-jackson-module-hibernate/" rel="nofollow">http://blog.pastelstudios.com/2012/03/12/spring-3-1-hibernate-4-jackson-module-hibernate/</a>. All this seems to work fine, but when I try using JSON Views so that the JSON contains only the fields in the view it does not work.</p> <p>The active view is always null. How do make the view active? I have tried for a day now with no luck...any help at all will be greatly appreciated. Thanks in advance.</p> <p>Below is the relavant code.</p> <p>Here is the Mapper</p> <pre><code>import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module; public class HibernateAwareObjectMapper extends ObjectMapper { public HibernateAwareObjectMapper() { Hibernate4Module hm = new Hibernate4Module(); registerModule(hm); hm.configure(Hibernate4Module.Feature.FORCE_LAZY_LOADING, true); } </code></pre> <p>} </p> <p>Here is the view class</p> <pre><code>public class DiffViews { public static class Public { } </code></pre> <p>}</p> <p>Here is the POJO where I use the view </p> <pre><code> @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id") @JsonIgnoreProperties(ignoreUnknown=true) @Entity public class Premium implements java.io.Serializable { @JsonView(DiffViews.Public.class) private String sequence; @JsonView(DiffViews.Public.class) @Column(name = "SEQUENCE", nullable = false, length = 4) public String getSequence() { return this.sequence; } public void setSequence(String sequence) { this.sequence = sequence; } @Column(name = "NAME", nullable = false) public String getName() { return this.name; } public void setName(String name) { this.name = name; } </code></pre> <p>)</p> <p>In my spring controller </p> <pre><code>@RequestMapping("/cartonPremium") public void listAll( @RequestParam("page") int page, @RequestParam("rows") int maxResults, @RequestParam("sidx") String sortKey, @RequestParam("sord") String sortOrder, HttpServletResponse response) { HibernateAwareObjectMapper mapper = new HibernateAwareObjectMapper(); mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.enable(SerializationFeature.WRITE_CHAR_ARRAYS_AS_JSON_ARRAYS); mapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION ); JqgridResponse&lt;Premium&gt; gridResponse = new JqgridResponse&lt;Premium&gt;(); gridResponse.setRows(premiumList); gridResponse.setRecords(""+premiumList.size()); gridResponse.setTotal(""+premiumList.size()); gridResponse.setPage(""+page); try { ObjectWriter objWriter= mapper.writerWithView(DiffViews.Public.class); Class&lt;?&gt; xxx = mapper.getSerializationConfig().getActiveView(); objWriter.writeValue(response.getOutputStream(), gridResponse); //mapper.writerWithView(DiffViews.Public.class).writeValue(response.getOutputStream(), gridResponse); } catch (JsonGenerationException e) { e.printStackTrace(); } catch (JsonMappingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } </code></pre> <p>My spring config </p> <pre><code> &lt;mvc:annotation-driven&gt; &lt;mvc:message-converters&gt; &lt;bean class="com.creata.json.MappingJackson2HttpMessageConverter"&gt; &lt;property name="objectMapper"&gt; &lt;bean class="com.creata.json.HibernateAwareObjectMapper" /&gt; &lt;/property&gt; &lt;/bean&gt; &lt;/mvc:message-converters&gt; &lt;/mvc:annotation-driven&gt; </code></pre>
 

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