在泽西岛编组通用类型 [英] Marshalling of generic types in Jersey

查看:166
本文介绍了在泽西岛编组通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要返回几个结果和结果总数的客户列表。我必须在具有不同实体的几个地方这样做,所以我希望有一个具有这两个属性的泛型类:

I need to return to client list of few results and total count of results. I have to do it on several places with different entities so I would like to have a generic class with these two attributes:

@XmlRootElement
public class QueryResult<T> implements Serializable {
    private int count;
    private List<T> result;

    public QueryResult() {
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }

    public int getCount() {
        return count;
    }

    public List<T> getResult() {
        return result;
    }
}

服务:

@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public QueryResult<TestEntity> findAll(
    QueryResult<TestEntity> findAll = facade.findAllWithCount();
    return findAll;
}

实体并不重要:

@XmlRootElement
public class TestEntity implements Serializable {
    ...
}

但这导致: javax.xml.bind.JAXBException:类test.TestEntity及其任何超类都是已知的。

返回集合很简单,但我不知道如何返回我自己的泛型类型。我尝试使用 GenericType 但没有成功 - 我认为它是集合的。

Returning of just collection is easy but I don't know how to return my own generic type. I tried to use GenericType but without success - I think it's ment for collections.

推荐答案

我使用 @XmlSeeAlso 注释解决了它:

@XmlSeeAlso(TestEntity.class)
@XmlRootElement
public class QueryResult<T> implements Serializable {
    ...
}

另一种可能性是使用 @XmlElementRefs

这篇关于在泽西岛编组通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆