只要您使用的是Jackson
2+,现在就已经内置了对此的支持。
Address类中:
@JsonDeserialize(builder = Address.Builder.class)
然后,您需要将此注释添加到您的
Builder类中:
@JsonPOJOBuilder(buildMethodName = "create", withPrefix = "set")
如果您想重命名要构建的Builder的create方法,并且要以而不是set作为前缀的Builder的setter,则可以跳过第二个注释。
完整示例:
@JsonDeserialize(builder = Address.Builder.class)public class Address{ private Address(Map<LocationType, String> components) ... @JsonPOJOBuilder(buildMethodName = "create", withPrefix = "set") public static class Builder { public Builder setCity(String value); public Builder setCountry(String value); public Address create(); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)