Vijay
Vijay

Reputation: 39

Messaging exception (com.sun.jersey.api.MessageException) while querying Jersey REST service

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

Answers (1)

Lokesh Gupta
Lokesh Gupta

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

http://howtodoinjava.com/2012/11/26/writing-restful-webservices-with-hateoas-using-jax-rs-and-jaxb-in-java/

Upvotes: 2

Related Questions