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 us assume as an example that a remote server exposes a web service for providing task details to the client through the TaskService
interface below.
Example 39.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, Seam REST builds and injects a client object of the web service.
Example 39.2. Injecting REST Client
@Inject @RestClient("http://example.com")
private TaskService taskService;
...
Task task = taskService.getTask(1);
The Seam REST module injects a
proxied TaskService
interface and 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.
URI definition supports EL expressions.
@Inject @RestClient("#{example.service.uri}")
Besides proxying JAX-RS interfaces, the RESTEasy Client Framework provides the ClientRequest API for manual building of HTTP requests. For more information on the ClientRequest API, refer to the project documentation.
Example 39.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 the default Apache HTTP Client 4 configuration. This can be altered by providing a ClientExecutor bean.
Example 39.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);
}