我正在尝试使用以下代码片段从http://api.openweathermap.org/data/2.5/forecast/daily?lat=35&lon=139&cnt=10&mode=json接收 json 数据:
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);
但最后一行抛出:
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。
pom.xml 中的 Jersey 依赖项:
<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>
此代码在应用程序服务器之外运行。谢谢。