簡體   English   中英

javax.ws.rs.WebApplicationException:javax.xml.bind.JAXBException,具有鏈接的異常類[Ljava.lang.Object;

[英]javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException with linked exception class [Ljava.lang.Object;

執行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實體

@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;
}

問題似乎是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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM