2010-09-14 15 views
5

दिखा मैं एक enum है:JAXB + Enums + एकाधिक मान

@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; 
    } 

है कि मैं एक बाकी सेवा में उत्पादन तो जैसे:

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> 

मैं इसे enum नाम (यानी, POKER) और कुंजी (यानी, "पसंदीदा-उत्पाद-पोकर") दोनों के साथ आउटपुट करना चाहता हूं।

मैंने इसे करने के कई अलग-अलग तरीकों का प्रयास किया है जिसमें @XmlElement, @XmlEnumValue और @XmlJavaTypeAdapter का उपयोग करके, दोनों एक ही समय में बाहर निकले बिना।

क्या कोई यह जानता है कि इसे कैसे प्राप्त किया जाए, जैसा कि आप सामान्य जेएक्सबी एनोटेटेड बीन के लिए करेंगे?

धन्यवाद।

उत्तर

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> 

आप के बजाय JAXB को ProductWrapper के उदाहरण से पारित करने की आवश्यकता होगी उत्पाद।

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

आप अपने enum मूल्य से @XmlEnum दूर करने के लिए, अगर आप चाहते हैं यह एक सामान्य वस्तु की तरह एक्सएमएल में धारावाहिक की जरूरत है। एक्सएमएल में एक सिंगल स्ट्रिंग प्रतीक द्वारा एक enum (परिभाषा के अनुसार) का प्रतिनिधित्व किया जाता है। यह इसे @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);}}; 
}