无法找到内容类型application / json的MessageBodyReader并输入类java.lang.String

时间:2022-06-11 18:03:47

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

我正在使用带有jackson提供程序的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 ClientResponseFailure exception 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);
}

7 个解决方案

#1


49  

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

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

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

#2


7  

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

问题实际上是RestEasy无法找到Jackson提供商。我不得不通过以下代码手动注册它:

   ResteasyProviderFactory instance=ResteasyProviderFactory.getInstance();
    RegisterBuiltin.register(instance);
    instance.registerProvider(ResteasyJacksonProvider.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应该扫描提供商并自动注册。

#3


1  

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

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

This file is included into resteasy-jackson-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-jackson-providers.jar中,但同样的文件也包含在另一个jar,restasy-jaxrs.jar和一个可执行的jar文件中,使用这两个jar,它们没有合并!!

#4


0  

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 Jackson (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(对象)。 JAX-RS(在您的情况下为RESTEasy)将自动调用Jackson(如果它已正确配置)并将您的对象序列化为json。然后在您的客户端,您将请求相同的类而不是字符串,一切都应该自己工作。我对ClientRequest / Response并不是特别熟悉所以它可能不像我说的那样有效;我们在客户端使用RESTEasy代理功能(参见ProxyFactory)。尽管如此,JAX-RS / RESTEasy也可以在客户端自动执行json序列化/反序列化,因此肯定是一种方式。

#5


0  

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,返回该对象)。

#6


0  

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

我有类似的问题,我意识到问题与resteasy-jackson-provider的版本有关。我刚刚从3.0.4.Final移动到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();

#7


0  

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

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

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson2-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:

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

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

In general it's enough to run the client.

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

#1


49  

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

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

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

#2


7  

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

问题实际上是RestEasy无法找到Jackson提供商。我不得不通过以下代码手动注册它:

   ResteasyProviderFactory instance=ResteasyProviderFactory.getInstance();
    RegisterBuiltin.register(instance);
    instance.registerProvider(ResteasyJacksonProvider.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应该扫描提供商并自动注册。

#3


1  

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

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

This file is included into resteasy-jackson-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-jackson-providers.jar中,但同样的文件也包含在另一个jar,restasy-jaxrs.jar和一个可执行的jar文件中,使用这两个jar,它们没有合并!!

#4


0  

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 Jackson (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(对象)。 JAX-RS(在您的情况下为RESTEasy)将自动调用Jackson(如果它已正确配置)并将您的对象序列化为json。然后在您的客户端,您将请求相同的类而不是字符串,一切都应该自己工作。我对ClientRequest / Response并不是特别熟悉所以它可能不像我说的那样有效;我们在客户端使用RESTEasy代理功能(参见ProxyFactory)。尽管如此,JAX-RS / RESTEasy也可以在客户端自动执行json序列化/反序列化,因此肯定是一种方式。

#5


0  

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,返回该对象)。

#6


0  

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

我有类似的问题,我意识到问题与resteasy-jackson-provider的版本有关。我刚刚从3.0.4.Final移动到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();

#7


0  

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

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

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jackson2-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:

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

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

In general it's enough to run the client.

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