在JAX-RS请求之间共享变量 [英] Share variables between JAX-RS requests

查看:140
本文介绍了在JAX-RS请求之间共享变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个关于JAX-RS的一个非常基本的问题,但我不知何故不能轻易找到答案。

I have what I think is a very basic question about JAX-RS but I somehow can't easily find the answer.

我正在尝试重构一个REST使用标准Javax servlet的服务 - 将请求手动路由到方法 - 进入更干净的JAX-RS实现。当前应用程序在servlet init()期间设置一些变量。它将它们分配为HttpServlet类的属性,以便它们在每个doGet()期间可用,并且可以作为参数传递给请求处理方法。为清楚起见,其中一个是作为缓存的ConcurentHashMap。

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.

现在,使用JAX-RS,我可以扩展Application来设置我的资源类。我还可以在每个资源实现中使用@Context注释,在处理请求时注入ServletContext之类的东西。但是我不知道如何在应用程序初始化期间类似地注入变量集。

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.

我正在使用JAX-RS的Apache Wink 1.3.0实现。

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

推荐答案

您可以使用侦听器用于初始化缓存并在Web应用程序启动之前将上下文设置为属性。如下所示:

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<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 并检索属性。

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<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;
    }

}

这篇关于在JAX-RS请求之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆