Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey Client message body reader for Java type not found

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!

like image 528
billsfan80 Avatar asked May 30 '26 07:05

billsfan80


1 Answers

add the @XmlRootElement annotation to your Result class.

like image 87
Alex Stybaev Avatar answered Jun 01 '26 22:06

Alex Stybaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!