854 Stimmen

Wie kann man Jackson anweisen, ein Feld bei der Serialisierung zu ignorieren, wenn sein Wert null ist?

Wie kann Jackson so konfiguriert werden, dass der Wert eines Feldes bei der Serialisierung ignoriert wird, wenn der Wert dieses Feldes null ist?

Zum Beispiel:

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; 
}

1voto

Wir haben viele Antworten auf diese Frage. Diese Antwort kann in einigen Szenarien hilfreich sein Wenn Sie die Nullwerte ignorieren wollen, können Sie NOT_NULL auf Klassenebene verwenden. wie unten

@JsonInclude(Include.NON_NULL)
class Foo
{
  String bar;
}

Manchmal müssen Sie die leeren Werte ignorieren, z.B. wenn Sie die arrayList initialisiert haben, aber keine Elemente in der Liste vorhanden sind, dann verwenden Sie die NOT_EMPTY-Anmerkung, um die leeren Felder zu ignorieren.

@JsonInclude(Include.NON_EMPTY)
class Foo
{
  String bar;
}

1voto

Nguyen Minh Hien Punkte 385

Fall eins

@JsonInclude(JsonInclude.Include.NON_NULL)
private String someString;

Fall zwei

@JsonInclude(JsonInclude.Include.NON_EMPTY)
private String someString;

Wenn someString null ist, wird sie in beiden Fällen ignoriert. Wenn someString ist "", wird es nur im zweiten Fall ignoriert.

Dasselbe gilt für List = null ou List.size() = 0

1voto

Rishab Jula Punkte 87

Versuchen Sie das -

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class XYZ {

    protected String field1;

    protected String field2;
 }

Und für Nicht-Null-Werte (auf Getter-/Klassenebene) -

@JsonSerialize(include=JsonSerialize.Inclusion.NON_EMPTY)

0voto

mekdev Punkte 468

Jackson 2.x+ verwenden

mapper.getSerializationConfig().withSerializationInclusion(JsonInclude.Include.NON_NULL);

0voto

atom88 Punkte 1229

Außerdem müssen Sie Ihre Vorgehensweise bei der Verwendung von Map myVariable ändern, wie in der Dokumentation beschrieben, um Nullen zu eliminieren:

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

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X