首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从公开的RESTeasy接口访问@Local

从公开的RESTeasy接口访问@Local
EN

Stack Overflow用户
提问于 2011-04-28 05:11:03
回答 1查看 2.6K关注 0票数 2

我所要做的应该是非常直截了当的,但到目前为止是不可能的。有人能告诉我如何从公开的RESTeasy接口访问@Local吗?我已经搜索了互联网的长度和宽度,我所能找到的只有同一个例子的变化。

我试图了解如何使用RESTeasy以正常方式访问会话bean。到目前为止,情况就是这样:

使用:

EJB 3

RESTeasy 2.1

已发布的EJB接口:

代码语言:javascript
运行
复制
@Local
@Path("RequestReport")
public interface EReport {
 
     @GET
     @Produces({"application/xml"})
     @Path("request")
     public String requestReport(@QueryParam("reportId") @DefaultValue("") String reportId,
                                 @QueryParam("reportName") @DefaultValue("") String reportName, 
                                 @QueryParam("reportType") @DefaultValue("") String reportType);
 
     }
}

BEAN 1:

代码语言:javascript
运行
复制
@Stateless
public class EReportRequest implements EReport {      
         
     @EJB 
     private ReplyStringLocal replyString; // THIS IS WHERE THE PROBLEM LIES.
 
     public String requestReport(@QueryParam("reportId") @DefaultValue("") String reportId,
                                 @QueryParam("reportName") @DefaultValue("") String reportName, 
                                 @QueryParam("reportType") @DefaultValue("") String reportType) {     
 
          return replyString.getReply(reportId, reportName, reportType);        
 
    }
}

未发布的EJB接口:

代码语言:javascript
运行
复制
@Local
public interface ReplyStringLocal { 
 
     public String getReply(String reportId, String reportName, String reportType);
 
}

BEAN 2:

代码语言:javascript
运行
复制
@Stateless
public class ReplyString implements ReplyStringLocal { 
 
     public String getReply(String reportId, String reportName, String reportType) {
 
          return "<response><reportId>" + reportId + "</reportId><reportName>" + reportName +
                 "</reportName><reportType>" + reportType + "</reportType></response>";
      } 
}

为了演示我的问题,这个示例是非常简化的。提前感谢您的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-29 04:26:55

For: JBoss 5、RESTeasy 2.1和EJB3。

好的,我终于得到了关于使用RESTeasy的EJB的全部故事。所以这里是:

A.您可以通过为会话bean提供RESTeasy路径注释和annotation注解来发布具有RESTful接口的会话bean。

接口:

代码语言:javascript
运行
复制
@Local
@Path("MessageMaker")
public interface MessageMakerLocal {

    @GET
    @Produces({"application/xml"})
    @Path("getMessage")
    public String getMessage(@QueryParam("message") @DefaultValue("") String message);

}

执行情况:

代码语言:javascript
运行
复制
@Stateless
public class MessageMakerImpl implements MessageMakerLocal {

    public String getMessage(@QueryParam("message") @DefaultValue("") String message) {
        return "Your Message: " + message;
    }
}

B.您不能在RESTeasy中使用@EJB注释,因此不可能使用来自已发布的POJO或已发布的EJB的@Local引用。因此,原始文章中提供的示例无效。

C.要从已发布的POJO或已发布的会话Bean访问会话Bean,您可以使用@Remote接口注释并JAR Bean类。在构建EAR文件时,将JAR添加到EAR的根,并在META/application.xml文件中添加对它的引用。

接口:

代码语言:javascript
运行
复制
@Remote
public interface MessageMakerRemote {

    public String getMessage(@QueryParam("message") @DefaultValue("") String message);

    }
}

执行情况:

代码语言:javascript
运行
复制
@Stateless
@RemoteBinding(jndiBinding = "MessageMakerRemote")
public class MessageMakerImpl implements MessageMakerRemote {

    public String getMessage(String message) {
        return "Your Message: " + message;
    }
}

在Application.xml中:

代码语言:javascript
运行
复制
<module>
    <java>MessageMaker.jar</java>
</module>

然后,您可以使用JNDI远程调用jar来引用它:

代码语言:javascript
运行
复制
@Local
@Path("Message")
public class Message {

    @GET
    @Path("requestMessage")
    public String requestMessage(@QueryParam("msg") @DefaultValue("") String msg){

        // I use a custom JNDI remote call handler class so my call to the JARed beans looks like this:
        return JNDIRemote.getRemote(com.message.ejb3.MessageMakerRemote.class).getMessage(msg);
    }
}

我希望RESTeasy的后续版本能够提供更好的EJB集成。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5813898

复制
相关文章

相似问题

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