2010-09-14 138 views
5

我有一個枚舉:JAXB +枚舉+顯示多個值

@XmlEnum 
@XmlRootElement 
public enum Product { 
    POKER("favourite-product-poker"), 
    SPORTSBOOK("favourite-product-casino"), 
    CASINO("favourite-product-sportsbook"), 
    SKILL_GAMES("favourite-product-skill-games"); 

    private static final String COULD_NOT_FIND_PRODUCT = "Could not find product: "; 

    private String key; 

    private Product(final String key) { 
     this.key = key; 
    } 

    /** 
    * @return the key 
    */ 
    public String getKey() { 
     return key; 
    } 

我在REST服務輸出像這樣:

GenericEntity<List<Product>> genericEntity = new GenericEntity<List<Product>>(products) { 
}; 
return Response.ok().entity(genericEntity).build(); 

並輸出這樣的:

<products> 
<product>POKER</product> 
<product>SPORTSBOOK</product> 
<product>CASINO</product> 
<product>SKILL_GAMES</product> 
</products> 

我希望它輸出枚舉名稱(即撲克)和密鑰(即「最喜歡的產品撲克」)。

我已經嘗試了許多不同的方法來做到這一點,包括使用@XmlElement,@XmlEnumValue和@XmlJavaTypeAdapter,而沒有同時獲得兩者。

有沒有人知道如何實現這一目標,就像您使用普通的JAXB註釋bean一樣?

謝謝。

回答

4

你可以爲此創建包裝對象,像:

import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlValue; 

@XmlRootElement(name="product") 
public class ProductWrapper { 

    private Product product; 

    @XmlValue 
    public Product getValue() { 
     return product; 
    } 

    public void setValue(Product value) { 
     this.product = value; 
    } 

    @XmlAttribute 
    public String getKey() { 
     return product.getKey(); 
    } 

} 

這將對應於以下XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<product key="favourite-product-poker">POKER</product> 

您將需要ProductWrapper的情況下傳遞給JAXB,而不是產品。

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 

public class Demo { 

    public static void main(String[] args) throws Exception { 
     JAXBContext jc = JAXBContext.newInstance(ProductWrapper.class); 

     ProductWrapper pw = new ProductWrapper(); 
     pw.setValue(Product.POKER); 

     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.marshal(pw, System.out); 
    } 

} 
-1

如果您想將它作爲普通對象序列化爲XML,則需要從枚舉值中除去@XmlEnum。枚舉(按定義)在XML中由單個字符串符號表示。這允許將它與@XmlList結合起來,例如,創建一個有效的,以空格分隔的項目列表。

2

您可以使用適配器:

import java.io.StringWriter; 
import java.util.ArrayList; 
import java.util.List; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlElementWrapper; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlSeeAlso; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlValue; 
import javax.xml.bind.annotation.adapters.XmlAdapter; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 

public class XmlEnumTest{ 

    public static void main(String...str) throws Exception{ 
     JAXBContext jc = JAXBContext.newInstance(ProductList.class); 
     StringWriter sw = new StringWriter(); 
     Marshaller marshaller = jc.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(new ProductList(),sw); 
     System.out.println(sw.toString()); 
    } 
} 

class ProductTypeAdaper extends XmlAdapter<ProductAdapter, Product> { 
    @Override 
    public Product unmarshal(ProductAdapter v) throws Exception { 
     return Product.valueOf(v.value); 
    } 

    @Override 
    public ProductAdapter marshal(Product v) throws Exception { 
     ProductAdapter result = new ProductAdapter(); 
     result.key = v.getKey(); 
     result.value = v.name(); 
     return result; 
    } 
} 

@XmlType 
class ProductAdapter{ 
    @XmlAttribute 
    public String key; 
    @XmlValue 
    public String value; 
} 

@XmlJavaTypeAdapter(ProductTypeAdaper.class) 
enum Product{ 
    POKER("favourite-product-poker"), 
    SPORTSBOOK("favourite-product-casino"), 
    CASINO("favourite-product-sportsbook"), 
    SKILL_GAMES("favourite-product-skill-games"); 

    private static final String COULD_NOT_FIND_PRODUCT = "Could not find product: "; 

    private String key; 

    private Product(final String key) { 
     this.key = key; 
    } 

    /** 
    * @return the key 
    */ 
    public String getKey() { 
     return key; 
    } 

} 

@XmlRootElement 
@XmlSeeAlso({Product.class}) 
class ProductList{ 
    @XmlElementWrapper(name="products") 
    @XmlElement(name="product") 
    private List<Product> list = new ArrayList<Product>(){{add(Product.POKER);add(Product.SPORTSBOOK);add(Product.CASINO);}}; 
}