JAX-RS + JBoss 7.1.1 + RESTEasy:使用CDI的NullPointException [英] JAX-RS + JBoss 7.1.1 + RESTEasy: NullPointException using CDI

查看:96
本文介绍了JAX-RS + JBoss 7.1.1 + RESTEasy:使用CDI的NullPointException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Java EE 6解决方案,并且还试图找出导致依赖项注入无法正常工作的根本原因...服务内部的NullPointerException(userDao行):

I'm developing a Java EE 6 solution and also trying to figure out the root cause of why the dependency injection is not working...NullPointerException inside my service (userDao line):

REST服务

@Path("rest")
public class UserRESTService {

    @EJB
    UserDAO userDao;

    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    @Path("{type}")
    public String getJqGridData(@PathParam("type") String type) {

        System.out.println("type: " + type);

        List<USUARIO> usuarios = userDao.findAll();
        int pageSize = 10;
        int pageNumber = ((int) usuarios.size()/pageSize)+1;

        JqGridData<USUARIO> data = new JqGridData<USUARIO>(pageNumber, 1, usuarios.size(), usuarios); 

        System.out.println("Grid Data: " + data.getJsonString());
        return data.getJsonString();
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
   <display-name>MyWeb</display-name>
   <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>

     <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <context-param>
            <param-name>javax.faces.application.CONFIG_FILES</param-name>
            <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>

        <context-param>
            <param-name>com.sun.faces.writeStateAtFormEnd</param-name>
            <param-value>false</param-value>
    </context-param> 

    <listener>
                <listener-class>
                        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
                </listener-class>
            </listener>

    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

     <servlet>
         <servlet-name>Resteasy</servlet-name>
         <servlet-class>
             org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
         </servlet-class>
         <init-param>
             <param-name>javax.ws.rs.Application</param-name>
             <param-value>bch.com.br.service.rest.RESTEasySingleton</param-value>
         </init-param>
     </servlet>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

       <servlet-mapping>
            <servlet-name>Resteasy</servlet-name>
          <url-pattern>/rest/*</url-pattern>
        </servlet-mapping>

</web-app>

REST Singleton

REST Singleton

public class RESTEasySingleton extends Application
    {
    private Set<Object> singletons = new HashSet();
    private Set<Class<?>> empty = new HashSet();

    public RESTEasySingleton() {
        this.singletons.add(new UserRESTService());
    }

    public Set<Class<?>> getClasses()
    {
        return this.empty;
    }

    public Set<Object> getSingletons()
    {
        return this.singletons;
    }
}

DAO

@Stateless
public class UserDAO extends  BaseDAO<USUARIO>{

    @PersistenceContext(unitName="MyJPA")
    private EntityManager em;

    public UserDAO(Class<USUARIO> entityClass) {
            super(entityClass);
    }

    public UserDAO() {
        super(USUARIO.class);
    }

    @SuppressWarnings("unchecked")
    public List<USUARIO> findAll() {

            Query q = getEntityManager().createQuery("SELECT e FROM " + USUARIO.class.getName()  + " e");
            List<USUARIO> list = (List<USUARIO>) q.getResultList();
            return list;
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

}

尽管如此,注入在另一个托管bean内仍能正常工作...

Although, the injection works fine inside another managed bean...

@ManagedBean
@RequestScoped
    @Named
    public class UserBean {

    public UserBean() {
        // TODO Auto-generated constructor stub
    }

    private List<USUARIO> users = new ArrayList<USUARIO>();
    private String name;


    @Transient
    @EJB
    UserDAO userDao;

    ...

}

有什么想法吗?

推荐答案

@RequestScoped添加到您的其余端点.我还将在您的web.xml中删除对resteasy引导程序的引用,因为您无需将其部署到EE6容器(应用程序扩展就足够了).

Add @RequestScoped to your rest endpoint. I would also remove the reference to the resteasy bootstrap in your web.xml, since you're deploying to an EE6 container it's not needed (the application extension is enough).

这篇关于JAX-RS + JBoss 7.1.1 + RESTEasy:使用CDI的NullPointException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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