gpt4 book ai didi

java - 获取 MessageBodyProviderNotFoundException : MessageBodyReader not found for media type=text/plain When output is JSON string

转载 作者:行者123 更新时间:2023-12-02 13:41:24 27 4
gpt4 key购买 nike

我正在尝试调用 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)

--- 我的主要方法公共(public)类运行{

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>

最佳答案

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

@Consumes(MediaType.APPLICATION_JSON)

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

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

接下来,我们想让它也处理内容类型:文本/纯文本响应。为此,我们从 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);

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

GitHub

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

致谢

我基于以下资源中找到的信息构建了此解决方案:

堆栈溢出

其他

关于java - 获取 MessageBodyProviderNotFoundException : MessageBodyReader not found for media type=text/plain When output is JSON string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42721905/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com