2

I have a jsf 2.0 application without Spring and I have implemented a cache as application scope which should be accessed from a Rest service. Now I would like to call the rest webservice which should check the cache, but when I want access it, it is always null.

I tried already Accessing FacesContext from Web Service and this one https://www.mkyong.com/jsf2/how-to-get-servletcontext-in-jsf-2/ , but it doesn't work for me.

@ManagedBean(eager=true)
@ApplicationScoped
public class CacheController implements Serializable{

    private static final long serialVersionUID = 123L;

    private Map<String, Cache> map = new HashMap<String, Cache>();

    public Map<String, Cache> getMap() {
        return map;
    }

    public void setMap(Map<String, Cache> map) {
        this.map = map;
    }

}


@Path("/service")
public class RestService {

    @POST
    @Path("anlieferung/kennzahlen")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getValueFromCache(String item) throws JSONException, ParseException {

                //is always null
        CacheController cacheController= (CacheController) getServletContext().getAttribute("cacheController");

                //is always null
        FacesContext context = FacesContext.getCurrentInstance();
            Application application = context.getApplication();
            CacheController cacheBean = application.evaluateExpressionGet(context, "#{cacheController}", CacheController.class);

        //doSomeStuff and check if the item is in the Cache (CacheController.getMap())
        }
}

I have initialised the cache before over the jsf application and it works. Now I would expected that I get the Cache Object through the FacesContent or ServletContext, but it is always null. Do I need to create something like a ServletListener? Can somebody give me an example? Thank you

ak4784
  • 33
  • 4
  • Does https://stackoverflow.com/questions/13623443/how-to-access-jsf-application-scoped-managed-beans-from-an-httpsessionlistener help? Effectively you need the servletContext in your rest code. – Kukeltje Apr 29 '19 at 11:04
  • Possible duplicate of [How to access JSF application-scoped managed beans from an HttpSessionListener?](https://stackoverflow.com/questions/13623443/how-to-access-jsf-application-scoped-managed-beans-from-an-httpsessionlistener) – Kukeltje Apr 29 '19 at 11:04
  • Hmmm that is also a duplicate of something else: https://stackoverflow.com/questions/2633112/get-jsf-managed-bean-by-name-in-any-servlet-related-class – Kukeltje Apr 29 '19 at 11:06
  • I understand now that I can not get the FacesContext, because I am not in the "JSF world". I don t understand why I should implement something in a httpSessionListener when the session is destroyed. In addition I also tried CacheController bean = (CacheController) getServletContext().getAttribute("cacheController"); but it is also always null. – ak4784 Apr 29 '19 at 12:33
  • Is there a reason your REST endpoint is not a CDI managed bean? You could make it `@RequestScoped` (due to its statelessness) and then you could just `@Inject` the cache into it. – Smutje Apr 29 '19 at 12:52
  • Nooo, that is not the intention of the duplicate. It contains the info on how to get it from servletcontext. But the question to which in turn it is a duplicate is the better one. The link I posted in the 'Hmmmmm''''. And if what you tried is null, then it is not in there under that name. Debug and dump all attributes... Or set a breakpoint... or... – Kukeltje Apr 29 '19 at 13:01
  • @Smutje is right... That is also indirectly mentioned in the duplicate of the dupli cate – Kukeltje Apr 29 '19 at 13:03
  • It's not possible for me to use CDI :/ Is there now way with ManagedBeans as described above. My servletContext is always null, tried so many different things now and also checked your examples, but it is always the same behaviour... – ak4784 May 08 '19 at 15:07

0 Answers0