1

RESTから返されるxmlがあります。次のようになります。

<customers>
    <customer>
        <addressline1>111 E. Las Olas Blvd</addressline1>
        <addressline2>Suite 51</addressline2>
        <city>Fort Lauderdale</city>
        <creditLimit>100000</creditLimit>
        <customerId>1</customerId>
        <discountCode>
            <discountCode>56</discountCode>
            <rate>0.00</rate>
        </discountCode>
        <email>jumbocom@gmail.com</email>
        <fax>305-777-4635</fax>
        <name>Kupa</name>
        <phone>305-777-4632</phone>
        <state>FL</state>
        <zip>
            <areaLength>22.0</areaLength>
            <areaWidth>23.0</areaWidth>
            <radius>50.0</radius>
            <zipCode>90-200</zipCode>
        </zip>
    </customer>
</customers>

リストをアンマーシャリングしようとしていますが、リストは常に空です。おそらくその理由は、Customerクラスでは、ZipとDiscountCodeが他のエンティティであるため、文字列としてXMLしかなく、そこからエンティティを作成できないためです。

これらは私のJavaクラスです:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customers 
{
   @XmlElement
   private List<Customer> customers = new ArrayList<Customer>();

   public List<Customer> getCustomers()
   {
      return customers;
   }
} 


@Entity
@Table(name = "customer")
@XmlRootElement(name = "customer")
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "customer_id")
    private Integer customerId;
    @Column(name = "name")
    private String name;
    @Column(name = "addressline1")
    private String addressline1;
    @Column(name = "addressline2")
    private String addressline2;
    @Column(name = "city")
    private String city;
    @Column(name = "state")
    private String state;
    @Column(name = "phone")
    private String phone;
    @Column(name = "fax")
    private String fax;
    @Column(name = "email")
    private String email;
    @Column(name = "credit_limit")
    private Integer creditLimit;
    @JoinColumn(name = "discount_code", referencedColumnName = "discount_code")
    @ManyToOne(optional = false)
    private DiscountCode discountCode;
    @JoinColumn(name = "zip", referencedColumnName = "zip_code")
    @ManyToOne(optional = false)
    private MicroMarket zip;

// later only setters and getters
}

そして、これは私がマーシャリングを解除する方法です:

JAXBContext context = JAXBContext.newInstance(Customers.class, Customer.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Customers customers = (Customers) unmarshaller.unmarshal(new StringReader(allXmlString));

私は同様のトピックを見てきましたが、それらのいずれかが私を助けてくれました。アンマーシャリング後のリストが常に空になるのはなぜですか?

手伝ってくれてありがとう。

4

1 に答える 1

3

フィールドのデフォルトのマッピングは、customersと呼ばれるXML要素を探しますcustomers。注釈で、@XmlElementと呼ばれるXML要素にマップすることを指定する必要がありますcustomer

@XmlElement(name="customer")
private List<Customer> customers = new ArrayList<Customer>();

お客様

以下は、フルCustomersクラスがどのように見えるかです。

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customers 
{
   @XmlElement(name="customer")
   private List<Customer> customers = new ArrayList<Customer>();

   public List<Customer> getCustomers()
   {
      return customers;
   }
} 

詳細については

于 2012-08-24T15:48:43.903 に答える