小编典典

在JAX-RS资源中获取ServletContext

tomcat

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

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

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

}

有什么办法可以让我掌握ServletContextJAX-RS资源吗?


阅读 290

收藏
2020-06-16

共1个答案

小编典典

此外,@Resource注释可能不起作用。尝试这个

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

直到您按下服务方法,注入才会发生

public class MyService {
    @Context ServletContext context;

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

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