Reputation: 14741
When I execute REST service method, I am getting the following exception, how can I resolve this? Not sure which class [Ljava.lang.Object
Any help is highly appreciable.
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;
}
}
Dept Entity
@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();
}
Response class
@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 method
@GET
@Produces({MediaType.APPLICATION_JSON})
public Response getEmployees() {
Response response = new Response();
response.setCode(MessageCode.SUCCESS);
response.setPayload(getEmployeeService().findAllEmployees());
return response;
}
Upvotes: 0
Views: 6640
Reputation: 12534
The problem seems to be that JAX is unable to marshal List to XML. You have properly defined an XML binding for the Emp class. It may be the case that JAXB cannot handle a collection of these object. Try this:
1) introduce a wrapper class around 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) Alter your REST method to return this new wrapper object rather than the raw list
EmpList emps = new EmpList();
emps.setEmpList(getEmployeeService().findAllEmployees());
response.setPayload(emps);
Upvotes: 1