javax.ws.rs.WebApplicationException:带有链接异常类的javax.xml.bind.JAXBException [Ljava.lang.Object;

时间:2013-11-24 11:54:42

标签: java hibernate rest jpa jersey

当我执行REST服务方法时,我收到以下异常,我该如何解决这个问题?不确定哪个class [Ljava.lang.Object 任何帮助都非常值得赞赏。

javax.ws.rs.WebApplicationException: javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.SAXException2: class [Ljava.lang.Object; nor any of its super 
class is known to this context.
javax.xml.bind.JAXBException: class [Ljava.lang.Object; nor any of its super
class is known to this context.]
at 
com.sun.jersey.core.provider.jaxb.AbstractRootElementProvider.writeTo
(AbstractRootElementProvider.java:155)
at com.sun.jersey.spi.container.ContainerResponse.write
(ContainerResponse.java:306)

Emp Entity

@XmlRootElement
@Entity
@Table(name = "EMP")
@XmlSeeAlso({Emp.class})
@NamedQueries({
    @NamedQuery(name = "Emp.findAllEmployees", query = "select e.empno AS empno,e.ename AS ename,e.job as job,e.mgr AS mgr,e.sal AS sal,e.comm as comm,e.dept.deptno as deptno  from Emp e left join e.dept  order by e.empno desc")
})
public class Emp implements java.io.Serializable {

    private short empno;
    private Dept dept;
    private String ename;
    private String job;
    private Short mgr;
    private Date hiredate;
    private Integer sal;
    private Integer comm;

    public Emp() {
    }

    public Emp(short empno) {
        this.empno = empno;
    }

    public Emp(short empno, String ename, Dept dept, String job, Short mgr, Date hiredate, Integer sal, Integer comm) {
        this.empno = empno;
        this.dept = dept;
        this.ename = ename;
        this.job = job;
        this.mgr = mgr;
        this.hiredate = hiredate;
        this.sal = sal;
        this.comm = comm;
    }

    @Id
    @Column(name = "EMPNO", unique = true, nullable = false, precision = 4, scale = 0)
    public short getEmpno() {
        return this.empno;
    }

    public void setEmpno(short empno) {
        this.empno = empno;
    }

    @ManyToOne(fetch=FetchType.LAZY,cascade = CascadeType.MERGE)
    @JoinColumn(name = "DEPTNO")
    public Dept getDept() {
        return this.dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Column(name = "ENAME", length = 10)
    public String getEname() {
        return this.ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    @Column(name = "JOB", length = 9)
    public String getJob() {
        return this.job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Column(name = "MGR", precision = 4, scale = 0)
    public Short getMgr() {
        return this.mgr;
    }

    public void setMgr(Short mgr) {
        this.mgr = mgr;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "HIREDATE", length = 7)
    public Date getHiredate() {
        return this.hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    @Column(name = "SAL", precision = 7)
    public Integer getSal() {
        return this.sal;
    }

    public void setSal(Integer sal) {
        this.sal = sal;
    }

    @Column(name = "COMM", precision = 7)
    public Integer getComm() {
        return this.comm;
    }

    public void setComm(Integer comm) {
        this.comm = comm;
    }

}

部门实体

@XmlRootElement
@XmlSeeAlso({Dept.class})
@Entity
@Table(name="DEPT")
public class Dept  implements java.io.Serializable {

     private byte deptno;
     private String dname;
     private String loc;
     private Set emps = new HashSet(0);

    public Dept() {
    }


    public Dept(byte deptno) {
        this.deptno = deptno;
    }
    public Dept(byte deptno, String dname, String loc, Set emps) {
       this.deptno = deptno;
       this.dname = dname;
       this.loc = loc;
       this.emps = emps;
    }

     @Id   
    @Column(name="DEPTNO", unique=true, nullable=false, precision=2, scale=0)
    public byte getDeptno() {
        return this.deptno;
    }

    public void setDeptno(byte deptno) {
        this.deptno = deptno;
    }


    @Column(name="DNAME", length=14)
    public String getDname() {
        return this.dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }


    @Column(name="LOC", length=13)
    public String getLoc() {
        return this.loc;
    }

    public void setLoc(String loc) {
        this.loc = loc;
    }

@OneToMany(fetch=FetchType.LAZY, mappedBy="dept")
public Set<Emp> getEmps() {
    return this.emps;
}

    public void setEmps(Set emps) {
        this.emps = emps;
    }

}

DAO Impl

@Override
    public List<Emp> findAllEmployees() {
return getEntityManager().createNamedQuery("Emp.findAllEmployees")
.getResultList();
}

响应级

@XmlRootElement
@XmlSeeAlso({Emp.class,Dept.class})
public class Response implements Serializable {

    private static final long serialVersionUID = 1L;

    public enum MessageCode {

        SUCCESS, ERROR, UNKNOWN
    }

    private MessageCode code;
    private String message;
    private List<Emp> payload;

    public MessageCode getCode() {
        return code;
    }

    public void setCode(MessageCode code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<Emp> getPayload() {
        return payload;
    }

    public void setPayload(List<Emp> payload) {
        this.payload = payload;
    }

REST方法

@GET
@Produces({MediaType.APPLICATION_JSON})
public Response getEmployees() {
    Response response = new Response();
    response.setCode(MessageCode.SUCCESS);
    response.setPayload(getEmployeeService().findAllEmployees());
    return response;
}

1 个答案:

答案 0 :(得分:1)

问题似乎是JAX无法将List封送到XML。您已为Emp类正确定义了XML绑定。可能是JAXB无法处理这些对象的集合的情况。试试这个:

1)在Emp

周围引入一个包装类
@XmlRootElement(name="Employess")
public class EmpList {

   private List<Emp> emps = new ArrayList<Emp>();

   public void setEmpList(List<Emp> facpList) {
      this.emps = facpList;
   }

   @XmlElement(name="Emp")
   public List<Emp> getEmpList() {
      return emps;
   }
}

2)更改REST方法以返回此新包装器对象而不是原始列表

EmpList emps = new EmpList();
emps.setEmpList(getEmployeeService().findAllEmployees());
response.setPayload(emps);