1

我正在使用 Dropwizard 提供 RESTful 服务。我期望的 JSON 如下所示:

{"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包含ApiFeaturedMerchants 列表的类。

@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”组)中获得额外的级别?我是否有错误的注释,或者我错过了一个/一些?

4

1 回答 1

1

这是 ObjectMapperFactory 上的设置:

ObjectMapperFactory objectMapperFactory = new ObjectMapperFactory();
objectMapperFactory.enable(SerializationFeature.WRAP_ROOT_VALUE);
objectMapper = objectMapperFactory.build();
于 2013-08-20T15:33:55.070 回答