Jersey message body reader not found in maven-built JAR

My application uses the REST interface (JAX-RS). When I run it in Eclipse, ok. Domain objects are annotated; I do not use XML files to display REST.

Now I created a stand-alone JAR using the maven-assembly-plugin module, which packs the application and all the dependencies into a single executable JAR file. This also works.

But when I launch the application and request the object from the server, Jersey complains that he cannot find the reader of the message body:

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 is happening?

EDIT: After I slept through the night, I noticed that he complains about JSON ... but I use only XML for serialization. It’s strange.

+5
source share
5 answers

I fixed the problem, and I probably know how :-)

My resources were annotated as follows:

@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. Perhaps this would be enough to just clean up the JSON and XML in the client, but I did not.

+1
source

I ran into the same problem by doing a search on stackoverflow, I found that adding jersery-json-1.x.jar to WEB-INF / lib, as suggested, this solution will solve the problem. Please give the Mikhail award !

+3

( eclipse , jar) , , maven maven jar . , lib, , , .

+1

(http://goo.gl/Mk9sZ). maven jersey-multipart jar 1.0.2 1.8 ( , .

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

, http://goo.gl/Mk9sZ

+1

Jersey Client 1 json.

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));

}

}

0

All Articles