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:
我正在尝试从http://api.openweathermap.org/data/2.5/forecast/daily?接收json数据。lat=35&lon=139&cnt=10&mode=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);
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.
messagebodyprovidernotfoundexception: MessageBodyReader没有在媒体类型=application/octet-stream中找到,type=class com.app.weather.providers.org.openweathermap.pojo.WeatherForecast. weatherforecast .
Jersey dependencies in pom.xml:
泽在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.
此代码在应用服务器之外运行。谢谢。
3 个解决方案
#1
3
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 your Configurable
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:
Jersey JSON支持是一组扩展模块,其中每个模块都包含需要注册到可配置实例(客户端/服务器)中的特性实现。有多个框架支持JSON处理和/或JSON-to- java绑定。下面列出的模块通过将各个JSON框架集成到Jersey来提供对JSON表示的支持。目前,Jersey集成了以下模块,提供JSON支持:
- MOXy - JSON binding support via MOXy is a default and preferred way of supporting JSON binding in your Jersey applications since Jersey 2.0. When JSON MOXy module is on the class-path, Jersey will automatically discover the module and seamlessly enable JSON binding support via MOXy in your applications. (See Section 4.3, “Auto-Discoverable Features”.)
- 通过MOXy - JSON绑定支持是自Jersey 2.0以来支持JSON绑定的默认和首选方式。当JSON MOXy模块位于类路径时,Jersey将自动发现模块,并通过MOXy在应用程序中无缝地支持JSON绑定。(参见第4.3节“自动发现特性”。)
- Java API for JSON Processing (JSON-P)
- JSON处理的Java API (JSON- p)
- Jackson
- 杰克逊
- Jettison
- 抛弃
for more information read chapter 9 of jersey documentation.
要了解更多信息,请阅读《泽西文档》第9章。
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.
Moxy是json媒体支持的推荐方法。MOXy media模块是不需要显式地在客户端/服务器中注册它的特性(MoxyJsonFeature)的模块之一。
To use MOXy as your JSON provider you need to add jersey-media-moxy module to your pom.xml file:
要使用MOXy作为JSON提供程序,需要向pom中添加jerchy -media- MOXy模块。xml文件:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.15</version>
</dependency>
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:
如果您没有使用Maven,请确保拥有所有需要的依赖项。看到jersey-media-moxy依赖性。您需要将这些jar文件添加到项目中,以便通过jerchy -media-moxy支持json媒体类型:
- jersey-media-moxy-2.15.jar
- jersey-media-moxy-2.15.jar
- org.eclipse.persistence.core-2.5.0-rc2.jar
- org.eclipse.persistence.core-2.5.0-rc2.jar
- org.eclipse.persistence.antlr-2.5.0.jar
- org.eclipse.persistence.antlr-2.5.0.jar
- org.eclipse.persistence.moxy-2.5.0.jar
- org.eclipse.persistence.moxy-2.5.0.jar
- jersey-entity-filtering-2.3.1.jar
- jersey-entity-filtering-2.3.1.jar
Usage:
some ordinary class:
一些普通的类:
public class MyJAXBBean{
private String name = "jack";
private int id = 12;
public MyJAXBBean() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
}
And a main class for running a jersey client example:
运行jersey客户端示例的一个主要类:
public static void main(String[] args) {
//ClientConfig cc = new ClientConfig().register(new JacksonFeature());
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8084/myhost/test");
Form form = new Form();
form.param("x", "foo");
form.param("y", "bar");
MyJAXBBean bean;
bean = target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE),
MyJAXBBean.class);
System.out.println(bean);
}
the json response of the server(http://localhost:8084/myhost/test
) must be in the following format:
服务器的json响应(http://localhost:8084/myhost/test)必须采用以下格式:
{"name":"haleh", "id":3}
#2
2
Solved by writting custom MessageBodyReader. https://jersey.java.net/documentation/latest/message-body-workers.html#d0e4072
通过编写自定义MessageBodyReader解决。https://jersey.java.net/documentation/latest/message-body-workers.html d0e4072
#3
2
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.
我也遇到过类似的问题,尝试用“应用程序/javascript”这样的媒体类型来解封JSON。我想对最初的答案做出贡献,给出一个例子,说明我是如何实现建议的解决方案的。您可以根据需要更改媒体类型。
@Provider
@Consumes("application/javascript")
public class MyMoxyJSONProvider extends MOXyJsonProvider {
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return mediaType.equals(MediaType.valueOf("application/javascript"));
}
}
#1
3
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 your Configurable
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:
Jersey JSON支持是一组扩展模块,其中每个模块都包含需要注册到可配置实例(客户端/服务器)中的特性实现。有多个框架支持JSON处理和/或JSON-to- java绑定。下面列出的模块通过将各个JSON框架集成到Jersey来提供对JSON表示的支持。目前,Jersey集成了以下模块,提供JSON支持:
- MOXy - JSON binding support via MOXy is a default and preferred way of supporting JSON binding in your Jersey applications since Jersey 2.0. When JSON MOXy module is on the class-path, Jersey will automatically discover the module and seamlessly enable JSON binding support via MOXy in your applications. (See Section 4.3, “Auto-Discoverable Features”.)
- 通过MOXy - JSON绑定支持是自Jersey 2.0以来支持JSON绑定的默认和首选方式。当JSON MOXy模块位于类路径时,Jersey将自动发现模块,并通过MOXy在应用程序中无缝地支持JSON绑定。(参见第4.3节“自动发现特性”。)
- Java API for JSON Processing (JSON-P)
- JSON处理的Java API (JSON- p)
- Jackson
- 杰克逊
- Jettison
- 抛弃
for more information read chapter 9 of jersey documentation.
要了解更多信息,请阅读《泽西文档》第9章。
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.
Moxy是json媒体支持的推荐方法。MOXy media模块是不需要显式地在客户端/服务器中注册它的特性(MoxyJsonFeature)的模块之一。
To use MOXy as your JSON provider you need to add jersey-media-moxy module to your pom.xml file:
要使用MOXy作为JSON提供程序,需要向pom中添加jerchy -media- MOXy模块。xml文件:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.15</version>
</dependency>
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:
如果您没有使用Maven,请确保拥有所有需要的依赖项。看到jersey-media-moxy依赖性。您需要将这些jar文件添加到项目中,以便通过jerchy -media-moxy支持json媒体类型:
- jersey-media-moxy-2.15.jar
- jersey-media-moxy-2.15.jar
- org.eclipse.persistence.core-2.5.0-rc2.jar
- org.eclipse.persistence.core-2.5.0-rc2.jar
- org.eclipse.persistence.antlr-2.5.0.jar
- org.eclipse.persistence.antlr-2.5.0.jar
- org.eclipse.persistence.moxy-2.5.0.jar
- org.eclipse.persistence.moxy-2.5.0.jar
- jersey-entity-filtering-2.3.1.jar
- jersey-entity-filtering-2.3.1.jar
Usage:
some ordinary class:
一些普通的类:
public class MyJAXBBean{
private String name = "jack";
private int id = 12;
public MyJAXBBean() {
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
}
And a main class for running a jersey client example:
运行jersey客户端示例的一个主要类:
public static void main(String[] args) {
//ClientConfig cc = new ClientConfig().register(new JacksonFeature());
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8084/myhost/test");
Form form = new Form();
form.param("x", "foo");
form.param("y", "bar");
MyJAXBBean bean;
bean = target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE),
MyJAXBBean.class);
System.out.println(bean);
}
the json response of the server(http://localhost:8084/myhost/test
) must be in the following format:
服务器的json响应(http://localhost:8084/myhost/test)必须采用以下格式:
{"name":"haleh", "id":3}
#2
2
Solved by writting custom MessageBodyReader. https://jersey.java.net/documentation/latest/message-body-workers.html#d0e4072
通过编写自定义MessageBodyReader解决。https://jersey.java.net/documentation/latest/message-body-workers.html d0e4072
#3
2
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.
我也遇到过类似的问题,尝试用“应用程序/javascript”这样的媒体类型来解封JSON。我想对最初的答案做出贡献,给出一个例子,说明我是如何实现建议的解决方案的。您可以根据需要更改媒体类型。
@Provider
@Consumes("application/javascript")
public class MyMoxyJSONProvider extends MOXyJsonProvider {
@Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return mediaType.equals(MediaType.valueOf("application/javascript"));
}
}