フィールドの値がnullの場合、シリアル化中にフィールド値を無視するようにJacksonを構成するにはどうすればよいですか。
例えば:
public class SomeClass {
// what jackson annotation causes jackson to skip over this value if it is null but will
// serialize it otherwise
private String someValue;
}
Jackson> 2.0を使用してnull値を持つプロパティのシリアル化を抑制するには、直接構成するObjectMapper
@JsonInclude
か、アノテーションを使用します。
mapper.setSerializationInclusion(Include.NON_NULL);
また:
@JsonInclude(Include.NON_NULL)
class Foo
{
String bar;
}
@JsonInclude
または、値がnullでない場合に属性が表示されるように、ゲッターで使用することもできます。
より完全な例は、マップ内のnull値とBean内のnullフィールドがJacksonを介してシリアル化されるのを防ぐ方法に対する私の回答にあります。
Jackson 2.x では、次を使用します。
@JsonInclude(JsonInclude.Include.NON_NULL)
次のマッパー構成を使用できます。
mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
2.5以降、次を使用できます:
mapper.setSerializationInclusion(Include.NON_NULL);
設定できますapplication.properties
:
spring.jackson.default-property-inclusion=non_null
またはapplication.yaml
:
spring:
jackson:
default-property-inclusion: non_null
http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
動作するはずです。
Include.NON_EMPTY
値が null でも空でもない場合、プロパティがシリアル化されることを示します。
Include.NON_NULL
値が null でない場合、プロパティがシリアル化されることを示します。
このルールを Jackson 2.6+ のすべてのモデルに追加する場合は、次を使用します。
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
オブジェクトのリストをシリアル化しようとしていて、そのうちの 1 つが null の場合、JSON に null アイテムを含めることになります。
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
結果は次のとおりです。
[{myObject},null]
これを取得するには:
[{myObject}]
次のようなことができます:
mapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object obj, JsonGenerator jsonGen, SerializerProvider unused)
throws IOException
{
//IGNORES NULL VALUES!
}
});
ヒント: DropWizard を使用してObjectMapper
いる場合は、Jersey で使用されているものを取得できます。environment.getObjectMapper()
これはかなり長い間私を悩ませてきましたが、ついに問題を発見しました。この問題は、間違ったインポートが原因でした。以前、私が使用していた
com.fasterxml.jackson.databind.annotation.JsonSerialize
これは廃止されました。インポートを次のように置き換えるだけです
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
そしてそれを次のように使用します
@JsonSerialize(include=Inclusion.NON_NULL)
Spring を使用する場合のグローバル構成
@Configuration
public class JsonConfigurations {
@Bean
public Jackson2ObjectMapperBuilder objectMapperBuilder() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
builder.serializationInclusion(JsonInclude.Include.NON_NULL);
builder.serializationInclusion(JsonInclude.Include.NON_EMPTY);
builder.failOnUnknownProperties(false);
return builder;
}
}
Case one
@JsonInclude(JsonInclude.Include.NON_NULL)
private String someString;
Case two
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String someString;
If someString
is null, it will be ignored on both of cases.
If someString
is "" it just only be ignored on case two.
The same for List = null
or List.size() = 0
また、ドキュメントで説明されているように Map myVariable を使用して null を削除する場合は、アプローチを変更する必要があります。
From documentation:
com.fasterxml.jackson.annotation.JsonInclude
@JacksonAnnotation
@Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER, TYPE})
@Retention(value=RUNTIME)
Annotation used to indicate when value of the annotated property (when used for a field, method or constructor parameter), or all properties of the annotated class, is to be serialized. Without annotation property values are always included, but by using this annotation one can specify simple exclusion rules to reduce amount of properties to write out.
*Note that the main inclusion criteria (one annotated with value) is checked on Java object level, for the annotated type, and NOT on JSON output -- so even with Include.NON_NULL it is possible that JSON null values are output, if object reference in question is not `null`. An example is java.util.concurrent.atomic.AtomicReference instance constructed to reference null value: such a value would be serialized as JSON null, and not filtered out.
To base inclusion on value of contained value(s), you will typically also need to specify content() annotation; for example, specifying only value as Include.NON_EMPTY for a {link java.util.Map} would exclude Maps with no values, but would include Maps with `null` values. To exclude Map with only `null` value, you would use both annotations like so:
public class Bean {
@JsonInclude(value=Include.NON_EMPTY, content=Include.NON_NULL)
public Map<String,String> entries;
}
Similarly you could Maps that only contain "empty" elements, or "non-default" values (see Include.NON_EMPTY and Include.NON_DEFAULT for more details).
In addition to `Map`s, `content` concept is also supported for referential types (like java.util.concurrent.atomic.AtomicReference). Note that `content` is NOT currently (as of Jackson 2.9) supported for arrays or java.util.Collections, but supported may be added in future versions.
Since:
2.0