Jersey client, JAXBContext used when application is executed with Maven, but not with java -jar

By : Kimble
Source: Stackoverflow.com
Question!

I'm implementing a rest client consuming json data using the Jersey 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 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 {
    ...
}
By : Kimble


Answers

Finally! This is probably the most annoying thing I've had to cope with this year!

Adding 1.4-SNAPSHOT versions of the jersey dependencies solved it.

    
By : Kimble


This video can help you solving your question :)
By: admin