Accessing @Local Session Bean from an exposed RESTeasy interface

烂漫一生 提交于 2019-12-06 13:32:19

For: JBoss 5, RESTeasy 2.1 and EJB 3.

Ok so I finally got the full story on EJBs with RESTeasy. So here it is:

A. You can publish a Session bean with a RESTful interface by giving it RESTeasy path annotation and EJB Session bean annotaion.

INTERFACE:

@Local
@Path("MessageMaker")
public interface MessageMakerLocal {

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

}

IMPLEMENTATION:

@Stateless
public class MessageMakerImpl implements MessageMakerLocal {

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

.

B. You cannot use @EJB annotation in RESTeasy so using a @Local Session bean reference from a published POJO or published EJB is out of the question. So the example provided in the original post is not valid.

.

C. To access a Session Bean from published POJOs or published Session Bean, you can use the @Remote interface annotation and JAR your Bean classes. When you are building your EAR file add the JAR to the root of your EAR and add a reference to it in your META-INF/application.xml file.

INTERFACE:

@Remote
public interface MessageMakerRemote {

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

    }
}

IMPLEMENTATION:

@Stateless
@RemoteBinding(jndiBinding = "MessageMakerRemote")
public class MessageMakerImpl implements MessageMakerRemote {

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

In Application.xml:

<module>
    <java>MessageMaker.jar</java>
</module>

You can then make reference to it using a JNDI remote call to your jar:

@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);
    }
}

I am hoping that later versions of RESTeasy will provide better integration of EJBs.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!