Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you can change your original model with Jackson annotations, here is the way how to achieve this.</p> <p>Jackson has <a href="https://github.com/FasterXML/jackson-databind/wiki/Serialization-Features" rel="nofollow noreferrer">WRITE_EMPTY_JSON_ARRAYS</a>. And you may turn off it with:</p> <pre><code>objectMapper.configure(SerializationConfig.Feature.WRITE_EMPTY_JSON_ARRAYS, false); </code></pre> <p>But it works only for <code>Arrays</code>, but not for <code>Collections</code>. But you may combine <code>@JsonProperty</code> with <code>@JsonIgnore</code>, something like the following:</p> <pre><code>//....getter, setter of Person class @JsonIgnore public List&lt;Event&gt; getEvents() { return events; } @JsonProperty("events") public Event[] getEventsArr() { return events.toArray(new Event[0]); } </code></pre> <p>And afterwards you'll have output, as you expected.</p> <p><strong>EDIT</strong>: If you are using SpringMVC, then you can configure your actual <code>ObjectMapper</code> with explicit reference in <code>mvc:annotation-driven</code>:</p> <pre><code>&lt;!-- Configures the @Controller programming model --&gt; &lt;mvc:annotation-driven&gt; &lt;mvc:message-converters&gt; &lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"&gt; &lt;property name="objectMapper" ref="objectMapper"/&gt; &lt;/bean&gt; &lt;/mvc:message-converters&gt; &lt;/mvc:annotation-driven&gt; &lt;!--custom Json Mapper configuration--&gt; &lt;bean name="objectMapper" class="org.springframework.http.converter.json.JacksonObjectMapperFactoryBean" autowire="no"&gt; &lt;property name="featuresToDisable"&gt; &lt;list&gt; &lt;!--Ignore unknown properties--&gt; &lt;value type="org.codehaus.jackson.map.SerializationConfig.Feature"&gt;WRITE_EMPTY_JSON_ARRAYS&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p><em>Offtop:</em> usually it's quite useful to specify explicit instance of <code>ObjectMapper</code>, because:</p> <ul> <li>you can configure it in the way you want</li> <li>you can use its reference through <code>@Autowired</code></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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