获取MessageBodyProviderNotFoundException:找不到媒体类型= text / plain的MessageBodyReader当输出为JSON字符串时

时间:2017-03-10 15:23:46

标签: java json jersey jackson

我正在尝试调用request.getEntity来返回自定义类型,但它看起来像是以文本/纯文本而不是JSON获取响应,这给了我以下错误。我通过返回一个字符串来测试get输出,并且我能够将json作为字符串。我不确定为什么会这样。也许我错过了某种依赖? get方法使用https://github.com/mulesoft-labs/raml-java-client-generator生成。

Exception in thread "main" org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=text/plain, type=class org.mule.example.resource.weatherdata.model.WeatherdataGETResponse, genericType=class org.mule.example.resource.weatherdata.model.WeatherdataGETResponse.
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:232)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:156)
at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:853)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:785)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:111)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:419)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:108)
at org.mule.example.resource.weatherdata.Weatherdata.get(Weatherdata.java:52)
at com.test.Run.main(Run.java:16)

---我的主要方法 公共类Run {

public static void main(String[] args) {
    WeatherdataGETQueryParam param = new WeatherdataGETQueryParam(-121.955236, 37.354108);
    WeatherdataGETHeader header = new WeatherdataGETHeader();
    header.setXMashapeKey("--some key---");
    WeatherdataGETResponse test =  MashapeWeatherAPIClient.create().weatherdata.get(param, header);
    System.out.println(test);


}

}

---我的获取命令

  public org.mule.example.resource.weatherdata.model.WeatherdataGETResponse get(WeatherdataGETQueryParam queryParameters, WeatherdataGETHeader headers) {
    WebTarget target = this.client.target(getBaseUri());
    if (queryParameters.getLng()!= null) {
        target = target.queryParam("lng", queryParameters.getLng());
    }
    if (queryParameters.getLat()!= null) {
        target = target.queryParam("lat", queryParameters.getLat());
    }
    final javax.ws.rs.client.Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON_TYPE);
    if (headers.getXMashapeKey()!= null) {
        invocationBuilder.header("x-mashape-key", headers.getXMashapeKey());
    }
    Response response = invocationBuilder.get();
    if (response.getStatusInfo().getFamily()!= Family.SUCCESSFUL) {
        Response.StatusType statusInfo = response.getStatusInfo();
        throw new MashapeWeatherAPIException(statusInfo.getStatusCode(), statusInfo.getReasonPhrase());
    }
    return response.readEntity(org.mule.example.resource.weatherdata.model.WeatherdataGETResponse.class);

}

我的依赖

<properties>
    <jersey.version>2.17</jersey.version>
</properties>
<dependencies>
    <!--Jersey client-->
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.4.1</version>
    </dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.24</version>
</dependency>       

    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>

1 个答案:

答案 0 :(得分:1)

首先让我们看看Jersey JAX-RS如何解析具有正确Content-Type的JSON响应:application / json。 当得到这样的响应时,它将查找javax.ws.rs.ext.MessageBodyReader接口的任何可用实现,该接口的注释为:

@Consumes(MediaType.APPLICATION_JSON)

这可以是任何实现,但是对于我们的示例,我们添加了MOXyJsonProvider作为项目的依赖项:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>${jersey.version}</version>
</dependency>

接下来,我们要使其也处理Content-Type:文本/纯文本响应。 为此,我们同时从MOXyJsonProvider继承了自定义响应阅读器,并使用MediaType.TEXT_PLAIN对其进行了注释:

@Provider
@Consumes(MediaType.TEXT_PLAIN)
public class MyCustomResponseReader extends MOXyJsonProvider {

    @Override
    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return genericType.equals(MyCustomClass.class);
    }

    @Override
    public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
            InputStream entityStream) throws IOException, WebApplicationException {
        return super.readFrom(type, genericType, annotations, mediaType, httpHeaders, entityStream);
    }

}

请注意,在重写的readFrom()方法中,我们要做的只是调用父类MOXyJsonProvider的super.readFrom()。

最后,我们需要在将查询我们的Web服务的javax.ws.rs.client.Client实例中注册自定义阅读器:

Client client = ClientBuilder.newBuilder().build().register(MyCustomResponseReader.class);

现在,文本/纯文本响应将像任何应用程序/ json一样被解析。

GitHub

完整的解决方案可以在GitHub上找到:PlainTextResponseReader

积分

我基于以下资源中的信息来解决此问题:

堆栈溢出

其他