Bal*_*yan 9 java json jersey jackson deserialization
要求 :
- 想用Builder模式
- 杰克逊进行反序列化
- 不想使用setter
我确信杰克逊的工作基于POJO上的吸气剂和制定者.既然,我有建造者模式,再没有重要的东西.在这种情况下,我们如何在Builder模式的帮助下指示jackson反序列化?
任何帮助,将不胜感激.我试过@JsonDeserialize(builder = MyBuilder.class)并且无法正常工作.
这在REST球衣中是必需的.我目前是杰克逊编组和解编的jersey-media-jackson maven模块.
mat*_*sev 16
@JsonDeserialize如果你有jackson-databind类路径,那就是要走的路.以下片段是从Jackson文档中复制的:
@JsonDeserialize(builder=ValueBuilder.class)
public class Value {
private final int x, y;
protected Value(int x, int y) {
this.x = x;
this.y = y;
}
}
public class ValueBuilder {
private int x, 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;
}
public Value build() {
return new Value(x, y);
}
}
Run Code Online (Sandbox Code Playgroud)
或者,@JsonPOJOBuilder如果您不喜欢具有with前缀的方法名称,请使用:
@JsonPOJOBuilder(buildMethodName="create", withPrefix="con")
public class ValueBuilder {
private int x, y;
public ValueBuilder conX(int x) {
this.x = x;
return this; // or, construct new instance, return that
}
public ValueBuilder conY(int y) {
this.y = y;
return this;
}
public Value create() { return new Value(x, y); }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7365 次 |
| 最近记录: |