I Have a rest app created with Jersey framework I'm trying to inject Stateful bean into my rest controller, but this bean always is created again. I'v tested this by passing test data into this bean after printing past data, but sout always printing null.
@Stateful
public class TestService {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
}
@Path("/testController")
public class TestController {
@EJB
private TestService testService;
@Path("/getTest/")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getPage(@QueryParam("TEST")String test) {
System.out.println(testService.getTest());
testService.setTest(test);
}
}
REST stands for Representational State Transfer. The "S" stands for State not "Stateless".
State Transfer: because REST Services are meant to tranfer the state of entities from client to server or vice versa.
Nevertheless REST Components have a stateless nature.
They are not bound to a specific client. BUT Stateful Session Beans are bound to a specific client. A stateful instance can for example be destroyed if the bound client stays inactive for an amount of time.
you try to inject a stateful session bean in a stateless component. This is not possible or at least not consisent at all.
A use case for the practice when you are using CDI (best issue):
\WEB-INF\beans.xmlfile and setbean-discovery-mode="all".Another use case when you are not using CDI: