2010-09-14 10 views
5

que tienen una enumeración:JAXB + enumeraciones + Mostrando varios valores

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

que la producción en un servicio REST así:

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

y emite la siguiente manera:

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

Quiero que muestre tanto el nombre de la lista (es decir, POKER) como la clave (es decir, "favorite-product-poker").

He intentado varias formas diferentes de hacerlo incluyendo el uso de @XmlElement, @XmlEnumValue y @XmlJavaTypeAdapter, sin obtener ambos al mismo tiempo.

¿Alguien sabe cómo lograr esto, como lo haría con un grano anotado JAXB normal?

Gracias.

Respuesta

4

Se puede crear un objeto contenedor para esto, algo como:

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(); 
    } 

} 

Esto correspondería a la siguiente XML:

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

que tendría que pasar a instancias de ProductWrapper a JAXB en lugar de Producto.

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

Es necesario eliminar el @XmlEnum de su valor de enumeración, si desea que se serializa en XML como un objeto normal. Una enumeración (por definición) se representa en el XML con un solo símbolo de cadena. Esto permite combinarlo con @XmlList, por ejemplo, para crear una lista de elementos eficiente, separada por espacios en blanco.

2

Se puede utilizar un adaptador:

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);}}; 
} 
Cuestiones relacionadas