1

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

1
  • Add the generic parameter type to your List return type. Commented Nov 15, 2012 at 12:48

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.