有没有办法将实体读取为 jax-rs 客户端?

Is there a way to read the entity as jax-rs client?

我刚刚制作了一个 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 提供商。对于 RESTeasy,您可以看到 this link 和 select 您的版本,并添加依赖项。

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

您好,您可以编写 RestEasy 或 Jersey Client 从您的服务中获取 Json。 如何写客户端你可以关注:http://entityclass.in/rest/jerseyClientGetXml.htm