問題があります。私はビルダーパターンをデシリアライズするためにjackson jsonの例を使用しましたが、私はいつも空のjsonを取得します。 私はjackson-databindバージョン2.8.4を使用しています 何か不足していますか? だから私のコードは次のとおりです。ビルダーパターンjson deserialize
バリュークラス
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;
}
}
ValueBuilderクラス
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);
}
}
スタートクラス
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);
}
}
なぜこれが下降しているのか分かりません... – Mena
デシリアライザは.readValueで使用されます。*シリアライザが必要な.writeValue *を呼び出しています。 – NateN