找不到媒体类型为application/octet-stream的MessageBodyReader [英] MessageBodyReader not found for media type=application/octet-stream

查看:178
本文介绍了找不到媒体类型为application/octet-stream的MessageBodyReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从

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);

但是最后一行抛出:

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.

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依赖项:

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支持是一组扩展模块,其中每个模块都包含一个Feature的实现,需要将其注册到您的Configurable实例(客户端/服务器).有多个框架提供对JSON处理和/或JSON到Java绑定的支持.下面列出的模块通过将各个JSON框架集成到Jersey中来提供对JSON表示的支持.目前,Jersey与以下模块集成以提供JSON支持:

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:

  • MOXy-自Jersey 2.0起,通过MOXy支持JSON绑定是在Jersey应用程序中支持JSON绑定的默认方法.当JSON MOXy模块位于类路径上时,Jersey将自动发现该模块,并通过MOXy在您的应用程序中无缝启用JSON绑定支持. (请参见第4.3节自动发现功能".)
  • 用于JSON处理的Java API(JSON-P)
  • 杰克逊
  • 杰蒂森

有关更多信息,请阅读球衣文档的第9章.

for more information read chapter 9 of jersey documentation.

Moxy是为json媒体提供支持的建议方法. MOXy媒体模块是不需要在客户端/服务器中显式注册其功能(MoxyJsonFeature)的模块之一,因此可配置,因为在将jersey-media-moxy模块添加到类路径时会自动发现并注册此功能.

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提供程序,您需要将jersey-media-moxy模块添加到pom.xml文件中:

To use MOXy as your JSON provider you need to add jersey-media-moxy module to your pom.xml file:

<dependency>
   <groupId>org.glassfish.jersey.media</groupId>
   <artifactId>jersey-media-moxy</artifactId>
   <version>2.15</version>
</dependency>

如果您不使用Maven,请确保具有所有必需的依赖项.参见 jersey-media-moxy依赖性.
您需要将这些jar文件添加到您的项目中,以便通过jersey-media-moxy支持json媒体类型:

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:

  • 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

一些普通班级:

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;
   }
}

还有一个用于运行jersey客户端示例的主类:

And a main class for running a jersey client example:

 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);
}

服务器(http://localhost:8084/myhost/test)的json响应必须采用以下格式:

the json response of the server(http://localhost:8084/myhost/test) must be in the following format:

 {"name":"haleh", "id":3}

这篇关于找不到媒体类型为application/octet-stream的MessageBodyReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆