如何使用私有构造函数和接口将JSON反序列化为Builder?

时间:2017-07-31 13:57:49

标签: java json jackson builder

我在互联网上搜索了类似于我正在做的事情,但却找不到任何东西。我在Java 8中使用接口创建了一个Builder模式,如下所示:

public class UrlImmutable {
   public final String parentUrl;
   public final Double parentUrlSentiment;
   public final Set<String> childUrls;
   public final boolean isParentVendorUrl;
   public final Map<TagClassification, Set<String>> parentUrlArticleTags;

private UrlImmutable(String parentUrl, Double parentUrlSentiment, Set<String> childUrls, boolean isParentVendorUrl, 
        Map<TagClassification, Set<String>> parentUrlArticleTags ) {
    super();
    this.parentUrl = parentUrl;
    this.parentUrlSentiment = parentUrlSentiment;
    this.childUrls = childUrls;
    this.isParentVendorUrl = isParentVendorUrl;
    this.parentUrlArticleTags = parentUrlArticleTags;
}

/** Our Interfaces for the Builder **/
public interface ParentUrlBuilder {
    ParentUrlSentimentBuilder parentUrl(String parentUrl);
}
public interface ParentUrlSentimentBuilder {
    ChildUrlBuilder parentUrlSentiment(Double parentUrlSentiment);
}
public interface ChildUrlBuilder {
    IsVendorUrlBuilder childUrls(Set<String> childUrls);
}
public interface IsVendorUrlBuilder {
    ParentUrlArticleTagsBuilder isParentVendorUrl(boolean isParentVendorUrl);
}
public interface ParentUrlArticleTagsBuilder {
    UrlImmutable parentUrlArticleTags(Map<TagClassification,Set<String>> parentUrlArticleTags);
}

public static ParentUrlBuilder discoveredUrl() {
    return parentUrl -> parentUrlSentiment -> childUrls -> isParentVendorUrl ->  parentUrlArticleTags -> 
    new UrlImmutable(parentUrl, parentUrlSentiment, childUrls, isParentVendorUrl, parentUrlArticleTags);
}

}

为了构建这个对象,我们这样做:

UrlImmutable url = 
    UrlImmutable()
        .parentUrl("http://www.google.com")
        .parentUrlSentiment(10.5)
        .childUrls(childUrls)
        .isParentVendorUrl(true)
        .parentUrlArticleTags(parentUrlArticleTags);

我似乎无法为此找到正确的注释组合。任何帮助非常感谢!

0 个答案:

没有答案