2013年1月9日水曜日

"MIME media type application/json was not found" (Jersey client)

If your Jersey client reports an error saying "MIME media type application/json was not found", try to enable FEATURE_POJO_MAPPING feature to support mapping between JSON and Java classes. Below is one example to create a Jersey client instance with FEATURE_POJO_MAPPING enabled.

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

private Client createJerseyClient()
{
    // Configuration of Jersey client.
    ClientConfig config = new DefaultClientConfig();

    // Enable mapping between JSON and Java classes.
    config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

    // Create a Client instance.
    return Client.create(config);
}

Note that jersey-json-*.jar is needed. If you are using Maven, add a dependency entry like below.

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.16</version>
</dependency>