ContainerRequestContext和HttpServletRequest之间的关系是什么?它们如何在JAX-RS中链接?

时间:2018-02-20 23:46:13

标签: java rest servlets jax-rs

JAX-RS过滤器在过滤请求时将ContainerRequestContext作为参数,但在实际的服务控制器中,我见过的大多数示例都以HttpServletRequest的形式获取其上下文,如

private HttpServletRequest httpServletRequest;

@Context
public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
    this.httpServletRequest = httpServletRequest;
}

有趣的是我可以在像

这样的过滤器中的ContainerRequestContext上设置属性
public void filter(ContainerRequestContext context){
    context.setProperty("MyProperty", "theValue");
}

然后在控制器中通过HttpServletRequest检索它,如

@POST
public Response post(Object param){
    if("theValue".equals(httpServletRequest.getProperty("MyProperty"))){
        // This works and evaluates true
    }
}

那么,ContainerRequestContext和HttpServletRequest是如何相关的呢?哪个是第一个,另一个是如何从中创建/更新的?

对于Response上下文/ servletRequest对象,它是否也都一样?

0 个答案:

没有答案