Reputation: 39
Context: -- Given the following code I am getting exception. Please tell me why is it happening with clear explanation:
@GET
@Produces("application/xml")
public List getEmployee()
{
List<Employee> emp=new ArrayList<Employee>();
return emp;
}
@XmlRootElement
public class Employee{
}
When I am calling getEmployee service I am getting following exception:
Caused by: com.sun.jersey.api.MessageException: A message body writer for Java class java.util.ArrayList, and Java type interface java.util.List, and MIME media type application/xml was not found ... 30 more
Thanks
Upvotes: 1
Views: 1481
Reputation: 472
You are retuning a list of Employees which an instance of ArrayList. You declared root annotation on Employee class not on arraylist.
You need to create a wrapper for holding the list of employees. This wrapper will enable you to create root element for list i.e.
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "users")
public class Users {
@XmlElement(name="user")
private ArrayList users;
public ArrayList getUsers() {
return users;
}
public void setUsers(ArrayList users) {
this.users = users;
}
}
Please refer to below tutorial for more understanding
Upvotes: 2