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

查看:29
本文介绍了在 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天全站免登陆