我有一个枚举:
@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>我希望它同时输出枚举名称(即POKER)和密钥(即“favourite product-poker”)。
我尝试了许多不同的方法,包括使用@XmlElement、@XmlEnumValue和@XmlJavaTypeAdapter,而不是同时使用这两种方法。
有没有人知道如何实现这一点,就像您对普通的带JAXB注释的bean一样?
谢谢。
发布于 2010-09-15 01:02:37
您可以为此创建一个包装器对象,如下所示:
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);
}
}发布于 2010-11-16 00:10:42
您可以使用适配器:
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);}};
}发布于 2010-10-19 22:46:14
如果希望像普通对象一样将枚举值序列化为XML,则需要从枚举值中删除@XmlEnum。在XML中,枚举(根据定义)由单个字符串符号表示。这允许将其与@XmlList结合使用,例如,创建一个有效的、以空格分隔的项目列表。
https://stackoverflow.com/questions/3708147
复制相似问题