在dubbo中我们需要获取HttpServletRequest或HttpServletResponse,对请求或是返回进行处理。
对于HttpServletRequest我们可以通过注解的方式进行获取,如下
public String getId( @Context HttpServletRequest request) { }
对于HttpServletResponse我们可以通过RpcContext进行获取,但是要注意的是只有在设置server="jetty"或server="tomcat"或server="servlet"才能通过以下的方式获取到。
if (RpcContext.getContext().getRequest() != null && RpcContext.getContext().getRequest() instanceof HttpServletRequest) { //强转 } if (RpcContext.getContext().getResponse() != null && RpcContext.getContext().getResponse() instanceof HttpServletResponse) { //强转 }