SeamFramework.orgCommunity Documentation
The RESTEasy Client Framework is a framework for writing clients for REST-based web services. It reuses JAX-RS metadata for creating HTTP requests. For more information about the framework, refer to the project documentation .
Integration with the RESTEasy Client Framework is optional in Seam REST and only available when RESTEasy is available on classpath.
Let's have the TaskService
sample interface. A remote server
implements this interface exposing a web service for getting task
details.
Example 5.1. Sample JAX-RS annotated interface
@Path("/task")
@Produces(application/xml)
public interface TaskService
{
@GET
@Path(/{id})
Task getTask(@PathParam(id)long id);
}
To access the remote web service, let Seam REST build and inject a client object of the web service.
Example 5.2. Injecting REST Client
@Inject @RestClient("http://example.com")
private TaskService taskService;
...
Task task = taskService.getTask(1);
What really happens is that the Seam REST module injects a
proxied TaskService
interface. Under the hood, the RESTEasy Client
Framework converts every method invocation on the TaskService
to an
HTTP request and sends it over the wire to http://example.com
. The
HTTP response is unmarshalled automatically and the response object
is returned by the method call.
In addition, it is possible to use EL expressions within the URI.
@Inject @RestClient("#{example.service.uri}")
Besides proxying JAX-RS interfaces, the RESTEasy Client Framework provides the ClientRequest API for building the HTTP requests manually. For more information on the ClientRequest API, refer to the project documentation
Example 5.3. Injecting ClientRequest
@Inject @RestClient("http://localhost:8080/test/ping")
private ClientRequest request;
...
request.accept(MediaType.TEXT_PLAIN_TYPE);
ClientResponse<String> response = request.get(String.class);
If not specified otherwise, every request is executed by a default Apache HTTP Client 4 configuration. This can be altered by providing a ClientExecutor bean.
Example 5.4. Custom Apache HTTP Client 4 configuration
@Produces
public ClientExecutor createExecutor()
{
HttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, 3);
ConnManagerParams.setTimeout(params, 1000);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
HttpClient httpClient = new DefaultHttpClient(cm, params);
return new ApacheHttpClient4Executor(httpClient);
}