Rather new to REST and Jersey, and I’m trying out some basic examples. I’ve got one particular question though, which I haven’t really found an answer for yet (don’t really know how to look for this): how would you go about storing/defining common services so that they are stateful and accessible to all/some resources?
For instance, a logger instance (Log4J or whatever). Do I have to manually initialize this and store it in the HttpSession? Is there a “best practice” way of doing this so that my logger is accessible to all/some resources?
Your best option is to inject the services into the Resource using something like Spring. Adding a setter in your Resource for the particular service makes it available for various scopes. The following tutorial explains in detail how to use Spring and Jersey. Below are examples of the various pieces needed to make this work. Specifically answering your question however the “best practice” is to allow Spring to manage the “stateful”ness or “scope” of a particular service.
Simple Service Bean
Simple JAX-RS Resource
Simple Spring applicationContext.xml, notice the “scope” for the Bean and Resource. This defines how stateful these objects are.