将EJB注入JAX-RS(RESTful服务) [英] Inject an EJB into JAX-RS (RESTful service)

查看:232
本文介绍了将EJB注入JAX-RS(RESTful服务)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过注释将一个无状态EJB 注入我的JAX-RS webservice。不幸的是,当我尝试使用它时,EJB只是 null ,并得到一个 NullPointerException

  @Path(book)
public class BookResource {

@EJB
private BookEJB bookEJB ;

public BookResource(){
}

@GET
@Produces(application / xml)
@Path( {bookId})
public Book getBookById(@PathParam(bookId)Integer id)
{
return bookEJB.findById(id);
}
}

我做错了什么?



以下是有关我的机器的一些信息:




  • Glassfish 3.1

  • Netbeans 6.9 RC 2

  • Java EE 6



你能显示一些工作吗例?

解决方案

我不知道这应该是有效的。所以要么:



选项1:使用注入提供者SPI



提供者将执行查找并注入EJB。请参阅:





com.sun.jersey的示例:jersey-server:1.17:

  import com.sun.jersey.core.spi.component.ComponentContext; 
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.ws.rs.ext.Provider;
import java.lang.reflect.Type;

/ **
* JAX-RS EJB注入提供商。
* /
@Provider
public class EJBProvider implements InjectableProvider&EJB,Type> {

public ComponentScope getScope(){
return ComponentScope.Singleton;
}

public Injectable getInjectable(ComponentContext cc,EJB ejb,Type t){
if(!(t instanceof Class))return null;

try {
Class c =(Class)t;
上下文ic = new InitialContext();

final Object o = ic.lookup(c.getName());

return new Injectable< Object>(){
public Object getValue(){
return o;
}
};
} catch(Exception e){
e.printStackTrace();
返回null;
}
}
}

选项2:使BookResource成为EJB

  @Stateless 
@Path(book)
公共类BookResource {

@EJB
私人BookEJB bookEJB;

// ...
}

请参阅: / p>



选项3:使用CDI

  @Path(book)
@RequestScoped
公共类BookResource {

@Inject
private BookEJB bookEJB;

// ...
}

请参阅: / p>


I'm trying to inject a Stateless EJB into my JAX-RS webservice via annotations. Unfortunately the EJB is just null and I get a NullPointerException when I try to use it.

@Path("book")
public class BookResource {

    @EJB
    private BookEJB bookEJB;

    public BookResource() {
    }

    @GET
    @Produces("application/xml")
    @Path("/{bookId}")
    public Book getBookById(@PathParam("bookId") Integer id)
    {
        return bookEJB.findById(id);
    }
}

What am I doing wrong?

Here is some information about my machine:

  • Glassfish 3.1
  • Netbeans 6.9 RC 2
  • Java EE 6

Can you guys show some working example?

解决方案

I am not sure this is supposed to work. So either:

Option 1: Use the injection provider SPI

Implement a provider that will do the lookup and inject the EJB. See:

Example for com.sun.jersey:jersey-server:1.17 :

import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;

import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.ws.rs.ext.Provider;
import java.lang.reflect.Type;

/**
 * JAX-RS EJB Injection provider.
 */
@Provider
public class EJBProvider implements InjectableProvider<EJB, Type> {

    public ComponentScope getScope() {
        return ComponentScope.Singleton;
    }

    public Injectable getInjectable(ComponentContext cc, EJB ejb, Type t) {
        if (!(t instanceof Class)) return null;

        try {
            Class c = (Class)t;
            Context ic = new InitialContext();

            final Object o = ic.lookup(c.getName());

            return new Injectable<Object>() {
                public Object getValue() {
                    return o;
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Option 2: Make the BookResource an EJB

@Stateless
@Path("book")
public class BookResource {

    @EJB
    private BookEJB bookEJB;

    //...
}

See:

Option 3: Use CDI

@Path("book")
@RequestScoped
public class BookResource {

    @Inject
    private BookEJB bookEJB;

    //...
}

See:

这篇关于将EJB注入JAX-RS(RESTful服务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆