Question

My application uses a REST (JAX-RS Jersey) interface. When I run it in Eclipse, everything' s fine. The domain objects are annotated, I'm not using XML files for the REST mapping.

Now I created a standalone JAR using the maven-assembly-plugin, which packs the application and all dependencies in a single, executable JAR file. This also seems to work.

But when I start the application and request an object from the server, Jersey complains, that it can't find a message body reader:

com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java type, class de.rybu.atuin.core.entity.User, and MIME media type, application/json, was not found

Any ideas why this happens?

EDIT: After I slept a night over it, I noticed that it complains about JSON... but I'm using only XML for serialization. Strange.

Was it helpful?

Solution 2

I fixed the problem and I guess I know how :-)

My resources were annotated like this:

@Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path(CONTEXT_ADDRESS)
public class UserResource
{
}

My client used the reverse order:

WebResource wr = ...
User user = wr.accept(MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_XML_TYPE).get(new GenericType<User>(){});

I don't know what initially caused the problem but I completely removed JSON-support and now it works. Maybe it would have been sufficient to simply swith the order of JSON and XML in the client but I didn't try that.

OTHER TIPS

I run into the same issue, searching on stackoverflow, I found that adding jersery-json-1.x.jar into WEB-INF/lib like suggested by this solution will solve the issue. Please give award to Mikhail!

I ran into a similar problem (worked fine running from eclipse or deployed as separate jars but not from executable jar) and found that this approach for creating executable jars using the maven dependency plugin and maven jar plugin works properly. This is because it puts the dependencies in a separate lib directory and then includes that in the classpath in the manifest as opposed to merging them all together which can cause numerous issues.

I faced the same issue (http://goo.gl/Mk9sZ) . It got solved by changing maven dependency for jersey-multipart jar from 1.0.2 to 1.8 version (Used the same dependency in client side as well as provider side.

             <dependency>
                <groupId>com.sun.jersey.contribs</groupId>
                <artifactId>jersey-multipart</artifactId>
                <version>1.8</version>
             </dependency>

You can find the complete code I used at http://goo.gl/Mk9sZ

I am using Jersey Client 1 and to solve this problem, I created a generic json message body reader.

public class JSONMessageBodyReader<T> implements MessageBodyReader<T> {

@Override
public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
        MediaType arg3) {
    return true;
}

@SuppressWarnings("unchecked")
@Override
public T readFrom(Class<T> clazz, Type type, Annotation[] arg2,
        MediaType arg3, MultivaluedMap<String, String> arg4,
        InputStream is) throws IOException, WebApplicationException {

    byte[] bytes = new byte[is.available()];
    is.read(bytes);
    String json = new String(bytes, "UTF-8");

    ObjectMapper mapper = new ObjectMapper();
    mapper.readValue(json, TypeFactory.defaultInstance().constructType(type));

    return (T) mapper.readValue(json, TypeFactory.defaultInstance().constructType(type));

}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top