I'm trying to receive json data from http://api.openweathermap.org/data/2.5/forecast/daily?lat=35&lon=139&cnt=10&mode=json with the following code snippet:
private WebTarget getWebTarget() {
Client client = JerseyClientBuilder.newClient();
return client.target("http://api.openweathermap.org/")
.path("data")
.path("2.5");
}
// new one method
Response response = getWebTarget()
.path("daily")
.queryParam("q", String.format("%s,%s", town, countryCode))
.queryParam("cnt", 10)
.queryParam("mode", "json")
.request().accept(MediaType.APPLICATION_JSON_TYPE).get();
WeatherForecast forecast = response.readEntity(WeatherForecast.class);
But last line throws:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/octet-stream, type=class com.app.weather.providers.org.openweathermap.pojo.WeatherForecast, genericType=class com.app.weather.providers.org.openweathermap.pojo.WeatherForecast.
Jersey dependencies in pom.xml:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.4</version>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json</artifactId>
<version>2.0-m05-1</version>
</dependency>
This code running outside of application server. Thanks.
Jersey JSON support comes as a set of extension modules where each of these modules contains an implementation of a
Feature
that needs to be registered into yourConfigurable
instance (client/server). There are multiple frameworks that provide support for JSON processing and/or JSON-to-Java binding. The modules listed below provide support for JSON representations by integrating the individual JSON frameworks into Jersey. At present, Jersey integrates with the following modules to provide JSON support:for more information read chapter 9 of jersey documentation.
Moxy is the proposed method for json media support. MOXy media module is one of the modules where you don't need to explicitly register it's Features (MoxyJsonFeature) in your client/server Configurable as this feature is automatically discovered and registered when you add jersey-media-moxy module to your class-path.
To use MOXy as your JSON provider you need to add jersey-media-moxy module to your pom.xml file:
If you're not using Maven make sure to have all needed dependencies. see jersey-media-moxy dependencies.
You need to add these jar file to your project in order to support json media types via jersey-media-moxy:
Usage:
some ordinary class:
And a main class for running a jersey client example:
the json response of the server(
http://localhost:8084/myhost/test
) must be in the following format:Solved by writting custom MessageBodyReader. https://jersey.java.net/documentation/latest/message-body-workers.html#d0e4072
I had a similar problem, tryinig to unmarshal JSON served with media type "application/javascript". I'd just like to contribute to the original answer, giving an example of how I implemented the suggested solution. You can change the media type according to your needs.