开发者

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

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 m开发者_Python百科essage 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 {
    ...
}


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.

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.4-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.4-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-apache-client</artifactId>
        <version>1.4-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.sun.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        <version>2.2.2-SNAPSHOT</version>
    </dependency>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜