【问题标题】:How to deploy EJB 3.1 session bean in a war on Websphere 8.5如何在 Websphere 8.5 上部署 EJB 3.1 会话 bean
【发布时间】:2013-02-23 11:29:03
【问题描述】:

现在我使用 Jersey 在 Websphere 8.5 上创建 RESTful Web 服务。
我还希望 RESTful Web 服务具有 EJB 3.1 的能力。
我的restful web服务代码如下:

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

当我在 Websphere 8.5 上部署战争时。 TagServiceRS 已成功部署为一个 RESTful Web 服务。我对其进行了测试。没关系。
但是 TagServiceRS 失败部署为 EJB 会话 bean。
TagServiceRS 的 entityManager 和 tagTestService 字段都是 null。
我看到日志,没有错误或警告日志。
下面是我的 TagTestServiceBean 代码。

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

}

如果有答案。非常感谢。

【问题讨论】:

标签: ejb jersey websphere jax-rs websphere-8


【解决方案1】:

将注解从@EJB改为@Resource

@Resource
private TagTestService tagTestService;

类本身不需要用@Stateless注解。

此外,JAX-RS 根资源和提供者类必须具有 JCDI 指定范围。范围控制 JCDI 托管 bean 的生命周期。根资源类可以具有任何有效范围,例如 @javax.enterprise.context.RequestScoped,这使得 JAX-RS 根资源类的行为与未启用 JCDI 的应用程序中的行为相同。 javax.ws.rs.core.Application 子类和 JAX-RS 提供者必须有 @javax.enterprise.context.ApplicationScoped 注解。

更多信息here。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 2013-04-30
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多