I am creating REST web service that need to be stateful. Consider following situation:
I am trying to do this via @Stateful
session bean but it still acts like @Stateless
. Now I am wondering wether it is even possible, because Client is not accepting any Cookie so server cannot identify it.
Is it possible to have Stateful bean accesed via REST?
Code sample:
@Path("/similarity/")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
@Stateful
@StatefulTimeout(600000) // 10 minutes
public class SimilarityResource {
private List<SimilarityResult> savedSimilarityResults = new ArrayList<SimilarityResult>();
@POST
@Path("/atom-count/")
public List<SimilarityResult> atomCountSimilarity(JAXBElement<SimilarityRequestXML> sr) {
try {
if (this.savedSimilarityResults.isEmpty()) {
List<SimilarityResult> similarityResults = acs.findAllSimilar(); // Time consuming
this.savedSimilarityResults = similarityResults; // Save results
return similarityResults;
} else {
CompoundResponse cr = new CompoundResponse("Hureeey stateful bean works!.", 404);
throw new WebApplicationException(cr.buildResponse());
}
} catch (CompoundSearchException e) {
CompoundResponse cr = new CompoundResponse(500, e);
throw new WebApplicationException(cr.buildResponse());
}
}
}
What I expect is, when I call this /atom-count/
method twice, it should response with 404.
您应该使用@SessionScoped
注释您的资源类,以告知JAX-RS创建具有会话生存期的请求对象,否则默认为@RequestScoped
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.