I’m using Jersey 1.1 (old, I know – have to because I’m stuck with Java 1.5). I’m doing a simple GET where a Java object is returned as the entity. The Java object is being marshalled correctly (Java to XML) because I can make the GET request via the web and it works great. I’m trying to use the Jersey client to make the GET request and unmarshall it back to the Java object and this is where it’s failing. Shouldn’t Jersey know how to unmarshall the XML it receives from the GET request back into the POJO since it’s annotated correctly? It works on the server side. Here’s the exception I get:
ClientHandlerException: A message body reader for Java type, class my.class.SearchResult, and MIME media type, application/xml was not found.
Here’s the client code:
private SearchResult search() {
WebResource wr = new Client().resource( "http://localhost:8080/MyProject/search" );
return wr.get( SearchResult.class );
}
Here’s the JAXB annotated POJO I’m using on the client and server:
@XmlRootElement(name = "searchResults")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "searchResults", propOrder = {
"results"
})
public class SearchResult {
@XmlElement(name = "result")
private List<Result> results = new ArrayList<Result>();
public List<Result> getResults() {
return results;
}
...
}
Here’s the inner Result POJO as well:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "resultType", propOrder = {
"name",
"version",
"type"
})
public class Result {
private String name;
private String version;
private String type;
...
}
Here’s the GET service itself:
@Path("/search")
public class SearchRest {
@GET
@Produces(MediaType.APPLICATION_XML)
public SearchResult search() {
SearchResult result = new SearchResult();
....
return result;
}
}
Thank you!
I fixed the issue by adding the JAXB jars to my classpath on the client side. I don’t know why this solved the problem but I would guess it’s because the @XmlRootElement and other JAXB annotations in my POJOs were simply ignored at runtime because the jars were not present. This resulted in Jersey not detecting the annotations and thus thinking it had no way of translating between the xml and POJO on the client side. I discovered the solution when I tried the alternative way that avoids the need for annotations, where you wrap the root POJO in a JAXBElement. I updated the web service class to produce a JAXBElement and updated the client to expect the same. Then I got a NoClassDefFoundError that was more indicative of the missing jars, since I was referencing JAXBElement directly. I guess I got lucky.