【问题标题】:Is there a way to read the entity as jax-rs client?有没有办法将实体读取为 jax-rs 客户端?
【发布时间】:2016-03-04 02:51:24
【问题描述】:

我刚刚创建了一个 jax-rs 服务,我正在尝试将从服务获得的字符串转换为实体。虽然使用 jax-rs 一切都是在服务器端自动完成的,但我认为也有可能在客户端完成它,但我没有找到它。

public class MyClient {

    public static void main(String[] args) {
        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget target = client.target("http://localhost:8080/restapp/api/paints/1");
        Response response = target.request().get();
        Paint values = response.readEntity(Paint.class);
        response.close();
    }
}

这给了一个e:

Exception in thread "main" javax.ws.rs.ProcessingException: RESTEASY003145: Unable to find a MessageBodyReader of content-type application/json and type class client.Paint

(它适用于字符串)。

【问题讨论】:

  • 你有 JSON 提供者吗?
  • @peeskillet 我只是对客户端应用程序有 resteasy 客户端依赖项。但是我没有在服务器端添加提供者之类的东西,所以我对你的问题有点困惑。
  • 如果您使用的是 Maven,请选择您的 RESTeasy 版本 here,并添加依赖项。如果您不使用 Maven,请参阅 this post
  • 你用的是什么服务器?如果您使用的是 Wildfly,它已经有提供程序。
  • @peeskillet 确实是这样,谢谢!顺便说一句,我可以用一块石头杀死两只鸟,你知道我如何将readEntity(Paint.class) 转换为接受 Paint 对象列表吗?

标签: web-services rest jax-rs resteasy rest-client


【解决方案1】:

您可以编写 ReastEasy 或 Jersy Client 从您的服务中获取 Json。 客户端怎么写可以关注:http://entityclass.in/rest/jerseyClientGetXml.htm

【讨论】:

    【解决方案2】:

    您需要添加 JSON 提供程序。对于 RESTeasy,您可以查看 this link 并选择您的版本,并添加依赖项。

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jackson2-provider</artifactId>
        <version>${resteasy3.version}</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      相关资源
      最近更新 更多