开发者

JAXB + Enums + Showing Multiple Values

开发者 https://www.devze.com 2023-01-15 21:41 出处:网络
I have an enum: @XmlEnum @XmlRootElement public enum Product { POKER(\"favourite-product-poker\"), SPORTSBOOK(\"favourite-product-casino\"),

I have an enum:

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

that I output in a REST service like so:

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

and it 开发者_如何学Pythonoutputs like this:

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

I want it to output with both the enum name (i.e, POKER) and the key (i.e, "favourite-product-poker").

I have tried a number of different ways of doing this including using @XmlElement, @XmlEnumValue and @XmlJavaTypeAdapter, without getting both out at the same time.

Does anyone know how to achieve this, as you would for a normal JAXB annotated bean?

Thanks.


You could create a wrapper object for this, something like:

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

}

This would correspond to the following XML:

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

You would need to pass instances of ProductWrapper to JAXB instead of Product.

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

}


You can use an adapter:

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


You need to remove the @XmlEnum from your enum value, if you want it to be serialized into XML like a normal object. An enum (by definition) is represented in the XML by a single string symbol. This allows combining it with @XmlList, for example, to create an efficient, whitespace-separated list of items.

0

精彩评论

暂无评论...
验证码 换一张
取 消