I have what I think is a very basic question about JAX-RS but I somehow can\'t easily find the answer.
I am trying to refactor a REST service which uses a \"standard
You can use a listener for init the cache and set to the context as attribute before the web application start. something like the following:
package org.paulvargas.shared;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class CacheListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
Map dummyCache = new HashMap();
dummyCache.put("greeting", "Hello Word!");
ServletContext context = sce.getServletContext();
context.setAttribute("dummyCache", dummyCache);
}
public void contextDestroyed(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
context.removeAttribute("dummyCache");
}
}
This listener is configured in the web.xml.
org.paulvargas.shared.CacheListener
restSdkService
org.apache.wink.server.internal.servlet.RestServlet
applicationConfigLocation
/WEB-INF/application
restSdkService
/rest/*
You can use the @Context annotation for inject the ServletContext and retrieving the attribute.
package org.apache.wink.example.helloworld;
import java.util.*;
import javax.servlet.ServletContext;
import javax.ws.rs.*;
import javax.ws.rs.core.*;
import org.apache.wink.common.model.synd.*;
@Path("/world")
public class HelloWorld {
@Context
private ServletContext context;
public static final String ID = "helloworld:1";
@GET
@Produces(MediaType.APPLICATION_ATOM_XML)
public SyndEntry getGreeting() {
Map dummyCache =
(Map) context.getAttribute("dummyCache");
String text = dummyCache.get("greeting");
SyndEntry synd = new SyndEntry(new SyndText(text), ID, new Date());
return synd;
}
}