Java 找不到 Media type=application/octet-stream 的 MessageBodyReader
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19846577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MessageBodyReader not found for media type=application/octet-stream
提问by user2966560
I'm trying to receive json data from http://api.openweathermap.org/data/2.5/forecast/daily?lat=35&lon=139&cnt=10&mode=jsonwith the following code snippet:
我正在尝试使用以下代码片段从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);
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.
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException:找不到 MessageBodyReader 媒体类型=应用程序/八位字节流,类型=类 com.app.weather.providers.org.openweathermap.pojo.WeatherForecast,genericType=class com.app .weather.providers.org.openweathermap.pojo.WeatherForecast。
Jersey dependencies in pom.xml:
pom.xml 中的 Jersey 依赖项:
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-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.
此代码在应用程序服务器之外运行。谢谢。
回答by user2966560
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
回答by Gio
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.
我有一个类似的问题,试图解组使用媒体类型“application/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"));
}
}
回答by Dariush Jafari
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 支持是一组扩展模块,其中每个模块都包含一个Feature
需要注册到您的Configurable
实例(客户端/服务器)中的实现。有多种框架支持 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”.)
- Java API for JSON Processing (JSON-P)
- Hymanson
- Jettison
- MOXy - 通过 MOXy 的 JSON 绑定支持是自 Jersey 2.0 以来在您的 Jersey 应用程序中支持 JSON 绑定的默认和首选方式。当 JSON MOXy 模块位于类路径上时,Jersey 将自动发现该模块并在您的应用程序中通过 MOXy 无缝启用 JSON 绑定支持。(请参阅第 4.3 节,“自动发现功能”。)
- 用于 JSON 处理的 Java API (JSON-P)
- Hyman逊
- 抛弃
for more information read chapter 9of 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 媒体模块是您不需要在客户端/服务器可配置中显式注册其功能 (MoxyJsonFeature) 的模块之一,因为当您将 jersey-media-moxy 模块添加到类路径时会自动发现并注册此功能.
To use MOXy as your JSON provider you need to add jersey-media-moxy module to your pom.xml file:
要将 MOXy 用作您的 JSON 提供程序,您需要将 jersey-media-moxy 模块添加到您的 pom.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 文件添加到您的项目中,以便通过 jersey-media-moxy 支持 json 媒体类型:
- jersey-media-moxy-2.15.jar
- org.eclipse.persistence.core-2.5.0-rc2.jar
- org.eclipse.persistence.antlr-2.5.0.jar
- org.eclipse.persistence.moxy-2.5.0.jar
- jersey-entity-filtering-2.3.1.jar
- jersey-media-moxy-2.15.jar
- org.eclipse.persistence.core-2.5.0-rc2.jar
- org.eclipse.persistence.antlr-2.5.0.jar
- org.eclipse.persistence.moxy-2.5.0.jar
- jersey-entity-filtering-2.3.1.jar
Usage:
用法:
some ordinary class:
一些普通的类:
public class MyJAXBBean{
private String name = "Hyman";
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:
以及用于运行球衣客户端示例的主类:
public static void main(String[] args) {
//ClientConfig cc = new ClientConfig().register(new HymansonFeature());
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:
server( http://localhost:8084/myhost/test
)的 json 响应必须采用以下格式:
{"name":"haleh", "id":3}