Search code examples
javarestjaxbjersey

Unable to return List of objects for the mediatype "application/xml" in rest api


Am trying to return list of object on HTTP GET request for the media type "application/xml".

Am getting below error :

SEVERE: MessageBodyWriter not found for media type=application/xml, type=class java.util.ArrayList, genericType=class java.util.ArrayList.

Tried below things :

1) My DTO has no arg constructor and implements seralizable. 2) I have jersey jaxb dependencies in my pom to do xml marshall/unmarshall.

Note: It does work with returning single object but not with List of objects

This might be a silly mistake.

@XmlRootElement
public class Company implements Serializable  {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String car;
    private String brand;

    public Company() {

    }
 ...... genereated setter/getter

I was getting List of objects from database.

// values getting from database.
    List<Company> companyList = new ArrayList<Company>(values);
    return Response.status(Status.OK).entity(companyList).build();

Help is Appreciated!


Solution

  • I was able to resolve this by adding GenericEntity and passing the list to it.

    GenericEntity<List<Company>> companyEntity = new GenericEntity<List<Company>>(companyList){};
    return Response.status(Status.OK).entity(companyEntity).build();