Question

I recently moved from Jersey 1.X to Jersey 2.1 and using jersey-media-json-jackson for (de-)serializing to Json.

In my JUnit-Test I would like to consume a web service that return a List. With Jerey 1.x I used to work with GenericType. However, folling code does not work with Jersey 2.1 / Jackson 2.1:

GenericType<Collection<String>> listType = new GenericType<Collection<String>>() {};
assertTrue(target("location").request().get(listType).contains("item"));

it crashes with

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: 
MessageBodyReader not found for media type=application/json, 
type=interface java.util.List, genericType=java.util.Collection<java.lang.String>.

Serializing is okay, since following code:

target("location").request().accept(MediaType.APPLICATION_JSON).get(String.class);

returns a valid Json String

Any ideas how to fix? Any working examples?


Versions:

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.1</version>
    </dependency>
Was it helpful?

Solution

Probably too late and you found the solution, but here it is for some else who might run into the same problem. Make sure you register the JacksonFeature on a client config before creating your client, code example below.

ClientConfig cc = new ClientConfig().register(new JacksonFeature());
Client client = ClientBuilder.newClient(cc);
WebTarget target = client.target(url);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top