问题
What I am trying to do should be very straight forward BUT thus far has been impossible. Can someone tell me how to access a @Local Session Bean from an exposed RESTeasy interface? I have trawled the length and breadth of the internet and all I can find is variations of the same example
I am trying to find out how I can access a session bean in the normal way using RESTeasy. This is what things look like so far:
USING:
EJB 3
RESTeasy 2.1
PUBLISHED EJB INTERFACE:
@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:
@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);
}
}
UNPUBLISHED EJB INTERFACE:
@Local
public interface ReplyStringLocal {
public String getReply(String reportId, String reportName, String reportType);
}
BEAN 2:
@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>";
}
}
This example is super simplified for the purposes of demonstrating my issue. Thanks in advance for any help.
回答1:
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.
来源:https://stackoverflow.com/questions/5813898/accessing-local-session-bean-from-an-exposed-resteasy-interface