6

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" Javax servlet -- routing requests to methods by hand -- into an "cleaner" JAX-RS implementation. The current application sets some variables during the servlet init(). It assigns those as attributes of the HttpServlet class so they are available during each doGet() and can be passed as parameters to request processing methods. For clarity, one of these is a ConcurentHashMap that acts as a cache.

Now, with JAX-RS, I can extend Application to set my resource classes. I can also use the @Context annotation in each resource implementation to inject things like ServletContext when processing a request. But I do not know how to similarly inject variables set during application initialization.

I am using the Apache Wink 1.3.0 implementation of JAX-RS.

4

1 回答 1

5

您可以使用侦听器来初始化缓存,并在 Web 应用程序启动之前将其设置为上下文作为属性。类似于以下内容:

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<String, String> dummyCache = new HashMap<String, String>();
        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");
    }

}

此侦听器在web.xml.

<listener>
    <listener-class>org.paulvargas.shared.CacheListener</listener-class>
</listener>
<servlet>
    <servlet-name>restSdkService</servlet-name>
    <servlet-class>
        org.apache.wink.server.internal.servlet.RestServlet
    </servlet-class>
    <init-param>
        <param-name>applicationConfigLocation</param-name>
        <param-value>/WEB-INF/application</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>restSdkService</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

您可以使用@Context注解来注入ServletContext和检索属性。

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<String, String> dummyCache = 
                       (Map<String, String>) context.getAttribute("dummyCache");

        String text = dummyCache.get("greeting");

        SyndEntry synd = new SyndEntry(new SyndText(text), ID, new Date());
        return synd;
    }

}
于 2013-04-19T17:26:04.660 回答