dropwizard:由一组项目产生的不正确的json

时间:2013-08-19 22:04:32

标签: json dropwizard

我使用Dropwizard提供RESTful服务。 JSON I EXPECT看起来像这样:

{"featuredMerchants":
    {"featuredMerchant":[
        {"browseId":"v1_0_0_1112",
            "merchantId":3902,
            "priority":1,
            "sourceId":"15"},
        ...,
        {"browseId":"v1_0_0_1112",
            "merchantId":456,
            "priority":4,
            "sourceId":"15"}]}}

但我正在获得的JSON是:

{"featuredMerchant":[
    {"browseId":"v1_0_0_1112",
        "merchantId":3902,
        "priority":1,
        "sourceId":"15"},
    ...,
    {"browseId":"v1_0_0_1112",
        "merchantId":456,
        "priority":4,
        "sourceId":"15"}]}

我有两节课。我有一个ApiFeaturedMerchantGroup类,其中包含ApiFeaturedMerchant s。

列表
@JsonRootName("featuredMerchants")
public class ApiFeaturedMerchantGroup {
    private List<ApiFeaturedMerchant> apiFeaturedMerchants;

    public ApiFeaturedMerchantGroup() {
    }

    @JsonProperty("featuredMerchant")
    public List<ApiFeaturedMerchant> getApiFeaturedMerchants() { return apiFeaturedMerchants; } 
    public void setApiFeaturedMerchants(List<ApiFeaturedMerchant> apiFeaturedMerchants) { this.apiFeaturedMerchants = apiFeaturedMerchants; }
}

@JsonRootName("featuredMerchant")
public class ApiFeaturedMerchant {

    private String browseId;
    private int merchantId;
    private Integer priority;
    private String sourceId;

    public ApiFeaturedMerchant() {
    }

    public String getBrowseId() { return browseId; }
    public void setBrowseId(String browseId) { this.browseId = browseId; }

    public int getMerchantId() { return merchantId; } 
    public void setMerchantId(int merchantId) { this.merchantId = merchantId; }

    public Integer getPriority() { return priority; } 
    public void setPriority(Integer priority) { this.priority = priority; }

    public String getSourceId() { return sourceId; } 
    public void setSourceId(String sourceId) { this.sourceId = sourceId; }
}

如何将额外级别添加到我的JSON中,即包含单个“featuredMerchant”项目的“featuredMerchants”组?我有错误的注释,或者我错过了一个/一些?

1 个答案:

答案 0 :(得分:1)

这是ObjectMapperFactory的设置:

ObjectMapperFactory objectMapperFactory = new ObjectMapperFactory();
objectMapperFactory.enable(SerializationFeature.WRAP_ROOT_VALUE);
objectMapper = objectMapperFactory.build();