【问题标题】:RestEasy client: How to wrap list elements?RestEasy 客户端:如何包装列表元素?
【发布时间】:2013-02-07 15:21:25
【问题描述】:

我正在尝试为 RestEasy REST 服务创建 RestEasy 客户端,提供此实体:

@SuppressWarnings("serial")
@Entity @Table(name="product")
@XmlRootElement(name="product")
public class Product implements Serializable, IHasTraits {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(columnDefinition = "INT UNSIGNED")
    private Long id;

结果是

[{"id":1,"name":"FOO","note":null,...}]

然后我尝试通过界面读取它。 我希望可以使用相同的模型类。

@Path("/")
public interface RestClient {

    @GET
    @Path("/products")
    @Produces("application/json")
    public List<Product> getProducts();

使用

    RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
    this.client = ProxyFactory.create(RestClient.class, "http://localhost:8080/essc-portal/rest");

但是客户端抛出:

引起:javax.xml.bind.UnmarshalException - 有链接异常:
[com.sun.istack.SAXParseException2;列号:0;意外元素(uri:“”,本地:“id”)。预期元素是 ]

我已经尝试添加

@XmlElementWrapper(name="product")

@XmlElement(type = Product.class, name = "product")

@org.jboss.resteasy.annotations.providers.jaxb.Wrapped(element = "product")

但两者都没有帮助。

我应该如何解决这个问题?我不想创建额外的 bean 类。

【问题讨论】:

    标签: rest jaxb client resteasy


    【解决方案1】:

    RestEasy 客户期望的是:

      [{"product":{"id":1,"name":"EAP","note":null,"extIdJira":null,"extIdBugzilla":"226"}]
    

    不是

      {"products":[{"id":1,"name":"EAP","note":null,"extIdJira":null,"extIdBugzilla":"226"}]}
    

    所以我最终改变了生产方法:

    public List<ProductWrapper> getProducts( @Context SecurityContext sc ) {
        final List<Product> prods = daoProd.getProducts_orderName(0);
        return rewrap(prods);
    }
    

    private List<ProductWrapper> rewrap(List<Product> prods) {
        List<ProductWrapper> p2 = new ArrayList(prods.size());
        for( Product product : prods){
            p2.add( new ProductWrapper(product));
        }
        return p2;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2017-09-23
      相关资源
      最近更新 更多