使用JAX-RS将Java嵌套类转换为JSON

时间:2015-08-03 21:05:03

标签: java android json jersey jax-rs

我基本上想从JAVA REST API向android发送一个类Customer的对象作为JSON。

我的Customer课程如下:

public class Customer {
    private long customerId;
    private String firstName;
    private String middleName;
    private String lastName;
    private String gender;
    private long accountId;

    class Account {
        private long accountId;
        private int balance;
    }
}

我期待的JSON应该是这样的:

{
 "customerId": "something",
 "firstName": "something",
 "middleName": "something",
 "gender": "M or F",
 "accountId": "something",
 "Account": {
  "accountId": "something",
  "balance": "something",
 } 
}

额外信息:

我使用此依赖项转换为JSON。

 <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>

如何创建此类的对象,以便JAX-RS的JSON转换看起来像这样?

1 个答案:

答案 0 :(得分:0)

外类可以有多个内部类实例。如果要将其编组为JSON,您应该告诉使用什么实例。为此,我想将Account account字段添加到外部类中。然后,您需要为私有字段和XmlElement注释添加getter / setter。最后,它看起来像这样:

import javax.xml.bind.annotation.XmlElement;

public class Customer {
    private long customerId;
    private String firstName;
    private String middleName;
    private String lastName;
    private String gender;
    private long accountId;
    private Account account;

    class Account {
        private long accountId;
        private int balance;

        @XmlElement(name = "accountId")
        public long getAccountId() {
            return accountId;
        }
        public void setAccountId(long accountId) {
            this.accountId = accountId;
        }
        @XmlElement(name = "balance")
        public int getBalance() {
            return balance;
        }
        public void setBalance(int balance) {
            this.balance = balance;
        }
    }

    @XmlElement(name = "customerId")
    public long getCustomerId() {
        return customerId;
    }
    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }
    @XmlElement(name = "firstName")
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    @XmlElement(name = "middleName")
    public String getMiddleName() {
        return middleName;
    }
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }
    @XmlElement(name = "lastName")
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    @XmlElement(name = "gender")
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    @XmlElement(name = "accountId")
    public long getAccountId() {
        return accountId;
    }
    public void setAccountId(long accountId) {
        this.accountId = accountId;
    }
    @XmlElement(name = "account")
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
}

现在,您可以将此类的实例与Jersey客户端一起使用,如下所示:

Entity e = Entity.entity(customer, "application/json");
Response r = invocationBuilder.method("POST", e);