How to get the ServletContext in Rest web service

后端 未结 2 1434
春和景丽
春和景丽 2021-02-13 20:39

I am implementing rest web service using Jersey. I need to have object of ServletContext to save the file in the application directory. Please help me to get the context.

<
相关标签:
2条回答
  • 2021-02-13 21:24

    use the annotation @context (Method level injection)

    public Response getContext(@Context HttpServletRequest req, @Context HttpServletResponse res)throws Exception
    {   
        System.out.println("Context Path is:"+req.getRequestURL().toString());
        result=req.getRequestURL().toString();
        return Response.status(200).entity(result).build();
    }
    
    0 讨论(0)
  • 2021-02-13 21:30

    Use @Context, here is Jersey documentation

    @Context
    private ServletContext context; 
    

    UPDATED - you can also inject directly into methods if desired

    public String uploadNotices(@Context ServletContext context, ...)
    
    0 讨论(0)
提交回复
热议问题