无法找到内容类型为 application/json 且类型为 java.lang.String 的 MessageBodyReader

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12175564/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:30:24  来源:igfitidea点击:

Unable to find a MessageBodyReader of content-type application/json and type class java.lang.String

jsonrestjax-rsHymansonresteasy

提问by user1632803

I am using RestEasy client with Hymanson providers and getting the above error

我正在使用带有 Hymanson 提供程序的 RestEasy 客户端并收到上述错误

clientside code is:

客户端代码是:

ClientRequest request = new ClientRequest(url);
request.accept(MediaType.APPLICATION_JSON);
ClientResponse<String> response = request.get(String.class);

if (response.getStatus() != 200) {
  throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
}

BufferedReader br =
  new BufferedReader(new InputStreamReader(new ByteArrayInputStream(response.getEntity().getBytes())));

response.getEntity()is throwing ClientResponseFailureexception with the error being

response.getEntity()正在抛出ClientResponseFailure异常,错误是

Unable to find a MessageBodyReader of content-type application/json and type class java.lang.String

My server side code is below:

我的服务器端代码如下:

@GET
@Path("/{itemId}")
@Produces(MediaType.APPLICATION_JSON)
public String item(@PathParam("itemId") String itemId) {
  //custom code

  return gson.toJSON(object);
}

回答by Dog

You could try to add the following dependency to your maven pom.

您可以尝试将以下依赖项添加到您的 maven pom.xml 文件中。

   <dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-Hymanson-provider</artifactId>
    <version>2.3.4.Final</version>
   </dependency>

回答by user1632803

The problem actually is that RestEasy is unable to find the Hymanson provider. I had to manually register it by the following code:

问题实际上是 RestEasy 无法找到 Hymanson 提供程序。我必须通过以下代码手动注册它:

   ResteasyProviderFactory instance=ResteasyProviderFactory.getInstance();
    RegisterBuiltin.register(instance);
    instance.registerProvider(ResteasyHymansonProvider.class);

Everything is working fine with this. But I am still unhappy with the solution as Resteasy is supposed to scan for the providers and register them automatically.

一切正常。但是我仍然对解决方案不满意,因为 Resteasy 应该扫描提供者并自动注册它们。

回答by Bruno Horta

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

回答by MatteoM

Just adding the line org.jboss.resteasy.plugins.providers.Hymanson.ResteasyHymansonProviderinto META-INF/services/javax.ws.rs.ext.Providers file, solves the problem.

只需将行org.jboss.resteasy.plugins.providers.Hymanson.ResteasyHymansonProvider添加到 META-INF/services/javax.ws.rs.ext.Providers 文件中,即可解决问题。

This file is included into resteasy-Hymanson-providers.jar but same file is also included into another jar, restasy-jaxrs.jar and for an executable jar file, that use both these jars, they are not merged !!

此文件包含在 resteasy-Hymanson-providers.jar 中,但相同的文件也包含在另一个 jar 中,restasy-jaxrs.jar,对于同时使用这两个 jar 的可执行 jar 文件,它们不会合并!

回答by JCalcines

I had a similar problem and I realized that the problem was related with the version of resteasy-Hymanson-provider. I just moved from 3.0.4.Finalto 3.0.5.Finaland the problem disappeared.

我有一个类似的问题,我意识到这个问题与resteasy-Hymanson-provider. 我刚从3.0.4.Finalto移动,3.0.5.Final问题就消失了。

Additionally I also realized that if I change the third line to the following the result was the expected with no need to change the dependencies.

此外,我还意识到,如果我将第三行更改为以下内容,则结果是预期的,无需更改依赖项。

Response response = request.get(Object.class).toString();

回答by Dmitry Svn

Things that had made work my code were that I added:

使我的代码工作的事情是我添加的:

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-Hymanson2-provider</artifactId>
    <version>&{resteasy.version}</version>
</dependency>

Beside this I don't know why but it seems that resteasy not initializing providers when client were created, this means that it required to init them manually:

除此之外,我不知道为什么,但似乎 resteasy 在创建客户端时没有初始化提供程序,这意味着它需要手动初始化它们:

 ResteasyProviderFactory instance=ResteasyProviderFactory.getInstance();
 RegisterBuiltin.register(instance);
 instance.registerProvider(ResteasyHymanson2Provider.class);

In general it's enough to run the client.

一般来说,运行客户端就足够了。

回答by Pascal Gélinas

I don't know the full rationale behind that but we've hit the exact same problem (multiple times :P) and you need to change the MediaType to TEXT_PLAIN.

我不知道这背后的全部原理,但我们遇到了完全相同的问题(多次:P),您需要将 MediaType 更改为TEXT_PLAIN.

Or you can also let JAX-RS do the job for you: instead of doing gson.toJSON(object), simply return object and change your method signature to whatever class that is. JAX-RS (RESTEasy in your case) will automatically call Hymanson (if it's properly configured) and serialize your object to json. Then on your client side you would request for that same class instead of String and everything should work on its own. I'm not particularly familiar with ClientRequest/Response so it might not work as I said; we use RESTEasy proxy functionality on the client side instead (see ProxyFactory). Nevertheless, JAX-RS/RESTEasy can do json serialize/deserialize automatically on the client side too so there's definetly a way.

或者您也可以让 JAX-RS 为您完成这项工作:而不是执行 gson.toJSON(object),只需返回 object 并将您的方法签名更改为任何类。JAX-RS(在您的情况下为RESTEasy)将自动调用Hymanson(如果配置正确)并将您的对象序列化为json。然后在您的客户端,您将请求相同的类而不是 String 并且一切都应该独立工作。我对 ClientRequest/Response 不是特别熟悉,所以它可能不像我说的那样工作;我们在客户端使用 RESTEasy 代理功能(参见 参考资料ProxyFactory)。尽管如此,JAX-RS/RESTEasy 也可以在客户端自动执行 json 序列化/反序列化,因此肯定有一种方法。

回答by StaxMan

If you really want to by-pass goodness of JAX-RS actually doing serialization for you (instead of manually calling GSON), use StreamingOutput(i.e. wrap outputter as StreamingOutput, return that object).

如果您真的想绕过 JAX-RS 的优点,实际上为您进行序列化(而不是手动调用 GSON),请使用StreamingOutput(即将输出器包装为StreamingOutput,返回该对象)。