在JAX-RS资源中获取ServletContext [英] Get ServletContext in JAX-RS resource

查看:142
本文介绍了在JAX-RS资源中获取ServletContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩JAX-RS,在Tomcat上部署。它基本上是:

I'm playing around with JAX-RS, deploying on Tomcat. It's basically:

@Path("/hello")
@Produces({"text/plain"})
public class Hellohandler {

    @GET
    public String hello() {
        return "Hello World";
    }

}

我有什么方法可以获得在我的JAX-RS资源中持有 ServletContext

Is there any way I can get hold of the ServletContext within my JAX-RS resource?

推荐答案

此外, @Resource 注释可能无法正常工作。试试这个

Furthermore, @Resource annotation might not work. Try this

@javax.ws.rs.core.Context 
ServletContext context;

在您点击服务方式之前注入不会发生

The injection doesn't happen until you hit the service method

public class MyService {
    @Context ServletContext context;

    public MyService() {
         print("Constructor " + context);  // null here     
    }

    @GET
    @Path("/thing") {               
             print("in  wizard service " + context); // available here

这篇关于在JAX-RS资源中获取ServletContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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