Question

Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: application/xml

I am getting the above error when trying to return a response in xml of a list of dynamically created classes/dtos.

@GET
@Path("objects")
public Response getObjects(
        @DefaultValue("json") @QueryParam("format") String format)
{    
     GenericEntity entity;

     //I use cglib here to dynamically at runtime create a class called objectDto.
     //The class is just a POJO.

     List<Object> objectsDto = generateObjects(fields);

     entity = new GenericEntity<List<Object>>(objectsDto){};

     Response.ResponseBuilder rBuild;

    if (format.equals("xml"))
    {
        rBuild = Response.ok(entity, MediaType.APPLICATION_XML);
    }
    else
    {
        rBuild = Response.ok(entity, MediaType.APPLICATION_JSON);
    }

    return rBuild.build();
}

The weird thing is I can return JSON representations of this object but not XML. Also I can return XML representations of not dynamically created classes.

I have the correct dependency in my Maven project for resteasy-jaxb-provider:

<dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>3.0.6.Final</version>
</dependency>
Was it helpful?

Solution

This is not possible using cglib out of the box. Since the XmlRootElement is not inherited, the subclass that is created by cglib will not longer carry this annotation. Cglib itself was written before annotations were introduced to Java and no recent update added this functionality. You can instead register an ASM visitor with the cglib enhancer which should be responsible for adding the annotation to the cglib generated class.

However, you might want to consider creating your classes using javassist which has a more modern API and supports the writing of annotations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top