I'm implementing a rest client consuming json data using the Jersy Client API. I don't really have much experience with JAXB, and especially not in combination with JSON. I've followed the example provided here (http://blogs.sun.com/enterprisetechtips/entry/consuming_restful_web_services_with) and registered a JAXBContext.
Everything works like a charm when I execute the project using mvn exec:java. I use the Maven assembly plugin to create a single jar. When I use this jar file to run the client the JAXBContext does not appear to be used at all. Which results in:
"com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class org.digitest.model.Account, and Java type class org.digitest.model.Account, and MIME media type application/json was not found"
Does anyone has any idea when I might be doing wrong? Even vague tips on how to proceed with debugging will be greatly appreciated!
@Provider
public final class ModelJAXBContextResolver implements ContextResolver<JAXBContext> {
private final JAXBContext context;
private final Set<Class> types;
private final Class[] cTypes = {
Account.class, ...
};
public ModelJAXBContextResolver() throws Exception {
this.types = new HashSet(Arrays.asList(cTypes));
this.context = new JSONJAXBContext(JSONConfiguration.natural().build(), cTypes);
throw new Exception("Json context is loaded");
}
@Override
public JAXBContext getContext(Class<?> objectType) {
return (types.contains(objectType)) ? context : null;
}
}
Client setup:
DefaultApacheHttpClientConfig config = new DefaultApacheHttpClientConfig();
config.getProperties().put(DefaultApacheHttpClientConfig.PROPERTY_HANDLE_COOKIES, true);
config.getClasses().add(ModelJAXBContextResolver.class);
client = ApacheHttpClient.create(config);
Account:
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Account {
...
}