2

Now I use Jersey to create restful web service on Websphere 8.5.
I also want the restful web service has the EJB 3.1's capacity.
My restful web service code as follow:

@Stateless
@Path("/tagServiceRS/{tagid}")
@Interceptors(TestTagServiceInterceptor.class)
public class TagServiceRS implements Serializable{
    private static final long serialVersionUID = 5L;
    private static final Logger log = LoggerFactory.getLogger(TagServiceRS.class);


    @EJB
    private TagTestService tagTestService;

    @PersistenceContext(unitName = "tag-ejb")
    private EntityManager entityManager;


    @GET
    @Produces("text/plain")
    public String findTagById(@PathParam("tagid") String tagid) {
       return "TAG";
    }


    /**
     * @return the tagTestService
     */
    public TagTestService getTagTestService() {
        return tagTestService;
    }

    /**
     * @param tagTestService the tagTestService to set
     */
    public void setTagTestService(TagTestService tagTestService) {
        this.tagTestService = tagTestService;
    }

When I deploy the war on the Websphere 8.5. The TagServiceRS was successful deployed as a restful web service.And I test it. It's OK.
But the TagServiceRS was failed deploy as a EJB session bean.
The TagServiceRS's entityManager and tagTestService fields are all null.
I see the log, there is no error or warning log.
Below is my TagTestServiceBean code.

@Stateless
public class TagTestServiceBean implements TagTestService, Serializable {

    private static final long serialVersionUID = 5L;
    private static final Logger log = LoggerFactory.getLogger(TagTestServiceBean.class);


    @Override
    public Tag testFindTagById(Long id) {
        log.info("testFindTagById ++++++++++++++++++++++++++++++++++++++++ invoked for id: {}", id);
        return new Tag();
    }

}

    @Remote
public interface TagTestService extends Serializable {

    /**
     * @param id
     *            the ID from database
     * @return a tag, may null
     */
    Tag testFindTagById(Long id);

}

If Any Answers.Thanks a lot.

FishGel
  • 1,100
  • 2
  • 16
  • 27
  • Maybe you can try the other approaches mentioned here: http://stackoverflow.com/questions/3027834/inject-a-ejb-into-jax-rs-restfull-service – Dror Bereznitsky Mar 16 '13 at 21:42

1 Answers1

0

Change the annotation from @EJB to @Resource

@Resource
private TagTestService tagTestService;

The class itself need not to be annotated with @Stateless annotation.

Additionally, JAX-RS root resource and provider classes must have a JCDI specified scope. Scopes control the lifecycle of a JCDI managed bean. Root resource classes can have any valid scope such as @javax.enterprise.context.RequestScoped, which makes the JAX-RS root resource class behave the same as in a non-JCDI enabled application. The javax.ws.rs.core.Application subclasses and JAX-RS providers must have the @javax.enterprise.context.ApplicationScoped annotation.

More info here.

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72