jackson を使用してデシリアライズするクラスがあり、そのクラスにはコレクション プロパティがあります。コレクションは空ですが、null ではありません。質問: クラスをデシリアライズする方法それなし空のコレクション。サンプルコードは以下の通りです。
class Person
{
String name;
List<Event> events;
//....getter, setter
}
もし
person.list = new List<Event>();
persion.name = "hello";
例外となる json は次のようになります。
{name: "hello"}
ない
{name: "hello", events:[]}
作り方は? よろしくお願いします〜〜
================================================
私はn1ckolasのアドバイスでこれを解決しました。まずはありがとうございます。私のジャクソンバージョンは2.1.1で、Spring-3.2.2インポートはこのバージョンのジャクソンをより良くサポートしています。また、これは両方で機能します。配列とコレクション。以下は私の設定です:
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="objectMapper"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--Json Mapper-->
<bean name="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" autowire="no">
<property name="featuresToDisable">
<list>
<!--not to return empty colletion-->
<value type="com.fasterxml.jackson.databind.SerializationFeature">WRITE_EMPTY_JSON_ARRAYS</value>
</list>
</property>
</bean>
ベストアンサー1
クラスまたはフィールドに @JsonInclude(JsonInclude.Include.NON_EMPTY) アノテーションを使用できます。
import com.fasterxml.jackson.annotation.JsonInclude;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
class Person
{
String name;
List<Event> events;
//....getter, setter
}