如何使用@XmlElement注释列表?
2022-09-01 09:25:08
我有以下注释使用:javax.xml.bind.annotation.XmlElement
@XmlElement
public List<String> getKeywords() {
return keywords;
}
当我编组一些示例内容时,它会生成以下XML:
<keywords>keyword1</keywords>
<keywords>keyword2</keywords>
我想得到下面的XML:
<keywords>
<keyword>keyword1</keyword>
<keyword>keyword2</keyword>
</keywords>
我应该使用哪种类型的注释?
我试过了
@XmlElementWrapper
@XmlElement(name="keyword")
但随后整个内容消失了,结果是:
<keywords/>
如果我只尝试重命名元素,也会发生同样的情况:
@XmlElement(name="keyword")
我做错了什么?
更新:
下面是根据第一个答案更新的类的完整代码,但它仍然不起作用(当编组为XML时,结果是一个空列表):<keywords/>
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Content {
private List<String> keywords;
public Content() {}
@XmlElementWrapper(name="keywords")
@XmlElement(name="keyword")
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
}
我还尝试了以下方法,结果相同:
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Content {
@XmlElementWrapper(name="keywords")
@XmlElement(name="keyword")
private List<String> keywords;
public Content() {}
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
}
但是,关键字不为空,因为下面生成的不是空列表:<keywords>keyword1</keywords><keywords>keyword2</keywords>
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Content {
private List<String> keywords;
public Content() {}
@XmlElement
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
}
编组的代码是 (JAX-RS):
import java.io.StringWriter;
import javax.ws.rs.Consumes;
import javax.ws.rs.Path;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
@Path("process")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_XML)
public class ContentHandler {
@POST
public Response process(Content content) {
StringWriter stringWriter = new StringWriter();
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Content.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(content, stringWriter);
} catch (JAXBException e) {
return Response.serverError().entity(e.getMessage()).build();
}
return Response.ok(stringWriter.toString(), MediaType.APPLICATION_XML).build();
}
}