0

我正在使用 Jersey 来提供 RESTful Web 服务。我使用 javax.xml.bind.annotation 来设置我的 POJO 数据传输对象。我的 DTO 包含一些其他 POJO,它们为主 DTO 提供值。我能够从浏览器中的资源中获取 JSON,万岁,但是 JSON 返回了更多对象的成员变量并且顺序错误。

这是资源:

@Controller
@Path("/merchants/{merchantId}/profile")
public class MerchantProfileResource {

    @Autowired
    private MerchantProfileManager merchantProfileManager;

    public MerchantProfileResource() {
    }

    @GET
    @Produces("application/json")
    // TODO - wire this up
    public MerchantProfileDTO getMerchantProfile(@PathVariable String id) {
        MerchantProfileDTO merchantProfile = merchantProfileManager.getMerchantProfileDTO(id);

        return merchantProfile;
    }

    public MerchantProfileManager getMerchantProfileManager() {
        return merchantProfileManager;
    }

    public void setMerchantProfileManager(MerchantProfileManager merchantProfileManager) {
        this.merchantProfileManager = merchantProfileManager;
    }
}

这是 DTO 类:

@XmlRootElement(name = "response")
@XmlType(propOrder={"merchantId", "email", "paymentMethods", "merchantTaxData"})
public class MerchantProfileDTO {

    @XmlElement(name = "merchantId")
    private int merchantId;

    public int getMerchantId() {
        if (merchant == null)
            return 0;
        return merchant.getMerchantid();
    }

    @XmlElement(name = "email")
    private String email;

    public String getEmail() {
        if (merchantProfile == null)
            return null;
        return merchantProfile.getEmail();
    }

    @XmlElementWrapper(name = "paymentMethods")
    @XmlElement(name = "paymentMethod")
    private List<PaymentMethod> paymentMethods;

    public List<PaymentMethod> getPaymentMethods() {
        return paymentMethods;
    }

    public void setPaymentMethods(List<PaymentMethod> paymentMethods) {
        this.paymentMethods = paymentMethods;
    }

    @XmlElement(name = "merchantTaxData")
    private MerchantTaxData merchantTaxData;

    public MerchantTaxData getMerchantTaxData() {
        return merchantTaxData;
    }

    public void setMerchantTaxData(MerchantTaxData merchantTaxData) {
        this.merchantTaxData = merchantTaxData;
    }

    private MerchantProfile merchantProfile;

    public MerchantProfile getMerchantProfile() {
        return merchantProfile;
    }

    public void setMerchantProfile(MerchantProfile merchantProfile) {
        this.merchantProfile = merchantProfile;
    }

    private Merchant merchant;

    public Merchant getMerchant() {
        return merchant;
    }

    public void setMerchant(Merchant merchant) {
        this.merchant = merchant;
    }
}

这是 MerchantProfile 类:

@javax.persistence.Table(name = "merchant_profile", schema = "", catalog = "mexp")
@Entity
public class MerchantProfile {

    private int merchantid;

    @javax.persistence.Column(name = "merchantid", nullable = false, insertable = true, updatable = true, length = 22, precision = 0)
    @Id
    public int getMerchantid() {
        return merchantid;
    }

    public void setMerchantid(int merchantid) {
        this.merchantid = merchantid;
    }

    private String email;

    @javax.persistence.Column(name = "email", nullable = true, insertable = true, updatable = true, length = 255, precision = 0)
    @Basic
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

这是商人类:

@javax.persistence.Table(name = "merchant", schema = "", catalog = "mexp")
@Entity
public class Merchant {

    private int merchantid;

    @javax.persistence.Column(name = "merchantid", nullable = false, insertable = true, updatable = true, length = 22, precision = 0)
    @javax.persistence.Id
    public int getMerchantid() {
        return merchantid;
    }

    public void setMerchantid(int merchantid) {
        this.merchantid = merchantid;
    }

    private MerchantProfile merchantProfile;

    @OneToOne
    @JoinColumn(name = "merchantid", unique = true, nullable = false, updatable = true)
    public MerchantProfile getMerchantProfile() {
        return merchantProfile;
    }

    public void setMerchantProfile(MerchantProfile merchantProfile) {
        this.merchantProfile = merchantProfile;
    }

    private Collection<MerchantNexus> merchantNexusesByMerchantid;

    @javax.persistence.OneToMany(orphanRemoval = true)
    @JoinColumn(name="merchantid")
    public Collection<MerchantNexus> getMerchantNexusesByMerchantid() {
        return merchantNexusesByMerchantid;
    }

    public void setMerchantNexusesByMerchantid(Collection<MerchantNexus> merchantNexusesByMerchantid) {
        this.merchantNexusesByMerchantid = merchantNexusesByMerchantid;
    }
}

MerchantTaxData 类:

@XmlRootElement(name = "merchantTaxData")
@XmlType(propOrder={"isTaxInfoKnown", "nexusList"})
public class MerchantTaxData {

    public MerchantTaxData() {
    }

    @XmlElement(name = "isTaxInfoKnown")
    private boolean isTaxInfoKnown;

    public boolean isTaxInfoKnown() {
        if (merchant == null)
            return false;
        return (1 == merchant.getIstaxinfoknown());
    }

    @XmlElementWrapper(name = "nexusList")
    @XmlElement(name = "nexus")
    private List<MerchantNexus> nexusList;

    public List<MerchantNexus> getNexusList() {
        return nexusList;
    }

    public void setNexusList(List<MerchantNexus> nexusList) {
        this.nexusList = nexusList;
    }

    private Merchant merchant;

    public Merchant getMerchant() {
        return merchant;
    }

    public void setMerchant(Merchant merchant) {
        this.merchant = merchant;
    }
}

我想要回来的是

{
    response: {
        merchantId: 92,
        email: "dev-catchall@blah.com",
        paymentMethods: [
            {paymentMethod: pm1},
            ...,
            {paymentMethod: pmN}
        ],
        merchantTaxData: {
            isTaxInfoKnown: true,
            nexusList: {
                nexus: [
                    {merchantid: 92,statecode: "CA"},
                    ...,
                    {merchantid: 92,statecode: "WA"}
                ]
            }
        }
    }
}

但我得到的远不止这些:

{
response: {
        merchantId: 0, 
        merchantTaxData: { 
            isTaxInfoKnown: true, 
            nexusList: {
                nexus: [
                    {merchantid: 92, statecode: "CA"}, 
                    ...,
                    {merchantid: 92, statecode: "WA"}
                ]
            }, 
            merchant: {
                merchantNexusesByMerchantid: [
                    {merchantid: 92, statecode: "CA"},
                    ...,
                    {merchantid: 92, statecode: "WA"}
                ],
                merchantProfile: {
                    email: "dev-mexp-catchall@pronto.com",
                    merchantid: 92
                },
                merchantId: 92
            }
        },
        merchant: {
            merchantNexusesByMerchantid: [
                {merchantid: 92, statecode: "CA"},
                ...,
                {merchantid: 92, statecode: "WA"}
            ],
            merchantProfile: {
                email: "dev-mexp-catchall@pronto.com",
                merchantid: 92
            }, 
            merchantid: 92
        }, 
        merchantProfile: {
            email: "dev-mexp-catchall@pronto.com", 
            merchantid: 92
        }
    }
}
4

2 回答 2

1

您可以使用 @XmlTransient 忽略某些属性。在您的 MerchantProfileDTO 类中,在属性 MerchantProfile 和商人中设置 @XmlTransient

@XmlTransient
private MerchantProfile merchantProfile;
@XmlTransient
private Merchant merchant;
于 2012-10-19T05:14:46.230 回答
0

我应该在 getter 之前有我的注释,而不是变量。

除此之外,@SamiKorhonen 或@user463324 在评论中说使用@XmlAccessorType(XmlAccessType.NONE) 是正确的方法。我邀请他们对这个问题提交一个真正的答案,但从来没有。

我不想因为不接受我的问题的答案而让社区感到悲伤。

于 2012-11-30T22:23:27.603 回答