首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAXB在解组后创建空的TreeSet

JAXB在解组后创建空的TreeSet
EN

Stack Overflow用户
提问于 2011-07-12 06:05:40
回答 1查看 375关注 0票数 2

我有一个RESTful服务客户机。服务返回SortedSet<Movie>,其中Movie是带注释的类。生成的XML如下所示。

在客户端,我有一个自定义的MessageBodyReader,如下所示。问题是Unmarshaller总是返回一个空的TreeSet

MovieSetMessageBodyReader.java

代码语言:javascript
复制
@SuppressWarnings("unchecked")
@Override
public TreeSet<Movie> readFrom(Class<TreeSet<Movie>> type,
    Type genericType, Annotation[] annotations, MediaType mediaType,
    MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
    throws IOException, WebApplicationException {
Class<Movie> baseType = Types.getCollectionBaseType(type, genericType);

try {
    JAXBContext ctx = JAXBContext.newInstance(type, baseType);
    JAXBElement<?> element = ctx.createUnmarshaller().unmarshal(
        new StreamSource(entityStream), type);

    return (TreeSet<Movie>) element.getValue();
} catch (JAXBException e) {
    e.printStackTrace();
}

return null;
}

MovieServiceRestEasyClient.java

代码语言:javascript
复制
@GET
@Consumes("text/xml")
public SortedSet<Movie> sendRequestByProxy() {
Map<String, Object> requestAttributes = new HashMap<String, Object>();
requestAttributes.put("path", path);

MovieServiceRestEasy proxy = ProxyFactory.create(
    MovieServiceRestEasy.class, ENDPOINT, requestAttributes);
ClientResponse<TreeSet<Movie>> response = (ClientResponse<TreeSet<Movie>>) proxy
    .getMovieSet(path);
return response.getEntity(TreeSet.class, (new GenericType<TreeSet<Movie>>() {}).getGenericType());

}

XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<collection>
<movie>     
    <genre>Unknown</genre>
    <name>4.3.2.1</name>
    <year>2010</year>
</movie>
<movie>     
    <genre>Unknown</genre>
    <name>Battlestar Galactica The Plan</name>
    <year>2009</year>
</movie>
</collection>
EN

回答 1

Stack Overflow用户

发布于 2012-06-15 05:11:34

为什么不使用POJO来保存未编组/编组的值呢?例如:

代码语言:javascript
复制
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement( name = "movie" )
public class Movie
{
    @XmlElement
    String name;

    @XmlElement
    String genre;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6657254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档