How do I configure JAX-RS 2 implementation (RESTEasy 3) to send the state of the application to the client?
In JSF I am able to do it using the STATE_SAVING_METHOD
parameter.
Is there a standard way of doing it using JAX-RS?
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
Just an example to illustrate my problem, I would like to configure the JAX-RS provider to return the cart
variable state to the client, in that way I won't keep the data in-memory or persist the session state.
@Path("/cart")
public class ShoppingCart {
// List of products presented to the user
private List<Product> availableProducts;
// Products selected by the user
private List<Product> cart;
@GET
public List<Product> addProduct() {
return availableProducts;
}
@POST
public void addProduct(Product product) {
cart.add(product);
}
}
Update
I would like to add references that support the idea of managing the state of the appliations. I agree that in theory, all services should be stateless, but in practice I found out that many scenarios would make more sense if the state could be maintained, specially for security and productivity reasons.
It seems that the pattern is usually done via hyperlinks, with the content properly encrypted to secure data and integrity. I am now looking into the JAX-RS tools that support these scenarios.
How to Manage Application State from the RESTful Web Services Cookbook:
Since HTTP is a stateless protocol, each request is independent of any previous request. However, interactive applications often require clients to follow a sequence of steps in a particular order. This forces servers to temporarily store each client’s current position in those sequences outside the protocol. The trick is to manage state such that you strike a balance between reliability, network performance, and scalability.
And also from the JAX-RS documentation:
Stateful interactions through hyperlinks: Every interaction with a resource is stateless; that is, request messages are self-contained. Stateful interactions are based on the concept of explicit state transfer. Several techniques exist to exchange state, such as URI rewriting, cookies, and hidden form fields. State can be embedded in response messages to point to valid future states of the interaction. See Using Entity Providers to Map HTTP Response and Request Entity Bodies and “Building URIs” in the JAX-RS Overview document for more information.