我刚刚创建了一个jax-rs服务,并试图将从该服务获得的String转换为实体。虽然使用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
(它适用于String)。
您需要添加一个JSON提供程序。对于RESTeasy,您可以看到此链接并选择您的版本,然后添加依赖项。
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson2-provider</artifactId>
<version>${resteasy3.version}</version>
</dependency>
hi您可以编写ReastEasy或Jersy Client从您的服务中获取Json。如何编写客户端,您可以遵循:http://entityclass.in/rest/jerseyClientGetXml.htm