JAX-RS Jersey:@XmlElementWrapper返回

时间:2013-10-14 11:56:49

标签: java-ee jersey jax-rs

我试图根据Jersey/Jaxb aliasing a List of beans

上接受的答案,在Artcile的评论列表周围添加一个包装类
public class Article implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    ...
    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "ARTICLE_COMMENT",
    joinColumns =
    {
        @JoinColumn(name = "ARTICLE_ID", referencedColumnName = "ID")
    },
    inverseJoinColumns =
    {
        @JoinColumn(name = "COMMENT_ID", referencedColumnName = "ID")
    })
    @XmlElementWrapper(name = "user_comments")
    private List<Comment> comments;

    public Article()
    {
    }
 ...
 }

而评论是

@XmlRootElement
@Entity
public class Comment implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;

    /*
    @ManyToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER)
    private Collection<Article> articles;
    */
    ...
}

然而它会返回此错误

Caused by: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Klasse enthält zwei Eigenschaften mit demselben Namen "comments"

将集合更改为评论列表并没有太大变化。任何人都知道我能做什么?

2 个答案:

答案 0 :(得分:8)

只是猜测 - 尝试注释getter而不是属性。

答案 1 :(得分:1)

或使用以下代码注释类:

#include "Wheel.h"  // Include Wheel's definition so it can be used in Car.
#include <vector>

class Wheel;

class Car
{
    std::vector<Wheel> wheels;
};

XML绑定是通过实例变量进行的,而无需使用getter或setter方法。 注释也可以用于非根元素。