首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何通过Jboss Resteasy拦截器实现?

如何通过Jboss Resteasy拦截器实现?
EN

Stack Overflow用户
提问于 2013-06-06 14:32:22
回答 1查看 475关注 0票数 0

我正在Jboss Resteasy API上工作,以便在Jboss服务器上实现REST服务。我是这个领域的新手。有人能帮帮我吗..。

有一个带有自定义注释(VRestAuto)的Rest服务方法,如下所示。

代码语言:javascript
运行
复制
@POST
@Produces("text/json")
@Path("/qciimplinv")
@Interceptors(VRestInterceptor.class)
public String getInvSummary(@VRestAuto("EnterpriseId") String enterpriseId,String circuitType){
   ....
   businessMethod(enterpriseId,circuitType);
   ....
}

@VRestAuto注解告诉我们'enterpriseId‘值在用户会话中可用。

用户只传递circuitType作为Rest客户机tool.Should中的POST参数,理想情况下,从会话中读取enterpriseid并使用这两个参数(enterpriseid、circuitType)调用Rest服务。

为了实现上述功能,实现了如下拦截器类(VRestInterceptor):

代码语言:javascript
运行
复制
public class VRestInterceptor implemnets PreProcessInterceptor,AcceptedByMethod  {
public boolean accept(Class declaring, Method method) {
      for (Annotation[] annotations : method.getParameterAnnotations()) {
             for (Annotation annotation : annotations) {
                 if(annotation.annotationType() == VRestAuto.class){
                     VRestAuto vRestAuto = (VRestAuto) annotation;
                    return vRestAuto.value().equals("EnterpriseId");
                 }
             }
         }
         return false;
     }
            Override
     public ServerResponse preProcess(HttpRequest request, ResourceMethod method)
             throws Failure, WebApplicationException {  ......} 
}

我能够在accept方法中验证VRestAuto注释。但是在preProcess方法中,如何使用两个参数(enterpriseid和circuitType)调用REST方法呢?

如果这些拦截器不适合,有没有其他拦截器最适合这个功能?

非常感谢您的帮助。

EN

回答 1

Stack Overflow用户

发布于 2013-06-20 04:31:35

为什么不忘记在调用方法时设置enterpriseId值,而只是注入HttpServletRequest并使用它来获取会话和值呢?

代码语言:javascript
运行
复制
@POST
@Produces("text/json")
@Path("/qciimplinv")
public String getInvSummary(String circuitType, @Context HttpServletRequest servletRequest) {
   HttpSession session = servletRequest.getSession();

   String enterpriseId = session.getAttribute("EnterpriseId").toString();

   ....
   businessMethod(enterpriseId,circuitType);
   ....
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16955187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档