use*_*560 14 java rest json jersey maven
我正在尝试使用以下代码片段从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);
Run Code Online (Sandbox Code Playgroud)
但最后一行抛出:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到媒体类型= application/octet-stream的MessageBodyReader,类型=类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>
Run Code Online (Sandbox Code Playgroud)
此代码在应用程序服务器之外运行.谢谢.
通过编写自定义MessageBodyReader解决。https://jersey.java.net/documentation/latest/message-body-workers.html#d0e4072
归档时间: |
|
查看次数: |
43987 次 |
最近记录: |