JAXB 在解组后创建空 TreeSet

发布于 2024-11-19 16:17:48 字数 2114 浏览 3 评论 0原文

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

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

MovieSetMessageBodyReader.java

@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

@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:

<?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>

I have a RESTful service client. The service returns SortedSet<Movie>, where Movie is a JAXB annotated class. The resulting XML is given below.

On the client side, I have a custom MessageBodyReader as given below. The issue is that the Unmarshaller always returns an empty TreeSet.

MovieSetMessageBodyReader.java:

@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:

@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:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红ご颜醉 2024-11-26 16:17:48

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

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;

Why don't you use POJO for holding the unmarshalled / marshalled values ? for example:

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文