czetsuya
czetsuya

Reputation: 5093

Invoke EJB from spring using rest jersey

First, the problem is when I invoke the ejb from the rest servlet, the ejb is always null.

I have a rest web service developed in jersey + spring 3.0.5. And an EJB 3.1 for services.

I have package the war and jar in an ear so my application looks like (I'm using maven for dependency):

+ear
++war
++jar

I was wondering how I could call the services in the jar file from my classes in the war file. As far as I remember it's through JNDI and I need to expose the ejb apis? How should I do that?

I'm sure the EJB are created successfully because I can see the log in the server like this: Portable JNDI names for EJB UserServiceBean : [java:global/demo-cg-ear-0.0.1-SNAPSHOT/demo-cg-ejbs/UserServiceBean!com.demo.cg.service.user.UserServiceBeanLocal, java:global/demo-cg-ear-0.0.1-SNAPSHOT/demo-cg-ejbs/UserServiceBean]|#]

But the problem is when I invoke it in the rest jersey servlet, it's always null:

@Path("/payment")
@Stateless
public class PaymentService {

@Path("/payment")
@Stateless
public class PaymentService {

    @EJB
    private UserServiceBeanLocal userServiceBean;

    @GET
    @Path("/hello")
    public Response savePayment() {

        String result = userServiceBean.getName();
        return Response.status(200).entity(result).build();

        /* return Response.status(200).entity("hello edward").build(); */
    }

}

My applicationContext.xml file

<context:annotation-config />
    <context:component-scan base-package="com.sido" />
    <context:property-placeholder location="WEB-INF/build.properties" />

    <!-- <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"> 
        <property name="alwaysUseJndiLookup" value="true" /> </bean> -->

    <jee:jndi-lookup id="userServiceBean"
        jndi-name="java:global/sido-cg-ear-0.0.1-SNAPSHOT/sido-cg-ejbs/UserServiceBean"
        resource-ref="true" lookup-on-startup="true"
        expected-type="com.sido.cg.service.user.UserServiceBeanLocal"
        proxy-interface="com.sido.cg.service.user.UserServiceBeanLocal"></jee:jndi-lookup>

UserBean class

@Interceptors(SpringBeanAutowiringInterceptor.class)
@Stateless
public class UserServiceBean implements UserServiceBeanLocal {
    private String name;

    public UserServiceBean() {
        name = "edward";
    }

    @PostConstruct
    private void init() {
        name = "edward";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Thanks,
czetsuya

Upvotes: 1

Views: 1169

Answers (1)

czetsuya
czetsuya

Reputation: 5093

For those who are interested this is how I did it: http://czetsuya-tech.blogspot.com/2012/05/how-to-call-stateless-ejb-from-spring.html

Upvotes: 1

Related Questions