Ich habe ein Problem. Ich habe gerade das Beispiel von jackson json benutzt, um das Builder-Muster zu deserialisieren, aber ich bekomme immer ein leeres json. Ich benutze Jackson-Databind Version 2.8.4 Fehle ich etwas? Also mein Code ist wie folgt:Builder Muster JSON deserialize
Der Wert Klasse
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize(builder=ValueBuilder.class)
public class Value {
private final int x, y;
protected Value(int x, int y) {
this.x = x;
this.y = y;
}
}
Die ValueBuilder Klasse
import com.fasterxml.jackson.annotation.JsonCreator;
//@JsonPOJOBuilder(buildMethodName = "build", withPrefix = "with")
public class ValueBuilder {
private int x;
private int y;
// can use @JsonCreator to use non-default ctor, inject values etc
public ValueBuilder() { }
// if name is "withXxx", works as is: otherwise use @JsonProperty("x") or @JsonSetter("x")!
public ValueBuilder withX(int x) {
this.x = x;
return this; // or, construct new instance, return that
}
public ValueBuilder withY(int y) {
this.y = y;
return this;
}
@JsonCreator
public Value build() {
return new Value(x, y);
}
}
Der Startklasse
public class Start {
public static void main(String[] args) throws IOException {
Value newValue = new ValueBuilder().withX(2).withY(4).build();
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(newValue);
System.out.println(jsonString);
}
}
Nicht sicher, warum dies downvoted ist ... – Mena
Ein Deserializer würde mit .readValue verwendet werden * Sie rufen .WriteValue *, die einen Serializer benötigen würde. – NateN