2017-06-07 88 views
0

我試圖讓CDI在我的應用程序中工作,但是我只做到了NullPointerExceptions。TomEE CDI @Inject NullPointerException

當正常實例化播放列表服務,它工作得很好,但是當我嘗試使用CDI時,它不再。

服務器能夠啓動,但是當我將beans.xml添加到META-INF /和WEB-INF /時,它也無法啓動。

PlaylistRESTController.java

import javax.inject.Inject; 
import javax.servlet.http.HttpServletRequest; 
import javax.ws.rs.*; 
import javax.ws.rs.core.*; 

@Path("/api/playlist") 
public class PlaylistRESTController { 

    @Inject 
    PlaylistService playlistService; //This one 

    @GET 
    @Produces(MediaType.APPLICATION_JSON) 
    public Playlist[] getUsersPlaylists(
      @CookieParam("st-session") Cookie session, 
      @Context HttpServletRequest httpServletRequest 
    ) { 
     User user = (User) httpServletRequest.getAttribute("user"); 

     Playlist[] playlist = playlistService.getAllPlaylists(user); 

     return playlist; 
    } 
} 

PlaylistService.java

public class PlaylistService implements IPlaylistService { 

    PlaylistJPA playlistJPA = new PlaylistJPA(); 

    public Playlist[] getAllPlaylists(User owner) { 
     ArrayList<Playlist> playlist = this.playlistJPA.findByOwner(owner); 

     return playlist.toArray(new Playlist[playlist.size()]); 
    } 
} 

的pom.xml

<dependencies> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-api</artifactId> 
     <version>7.0</version> 
    </dependency> 
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>6.0.6</version> 
     <scope>compile</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>4.2.6.Final</version> 
    </dependency> 
</dependencies> 

編輯:
資源/ META-INF/persistence.xml中

<?xml version="1.0" encoding="UTF-8"?> 

<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
    http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
    <persistence-unit 
      name="mysql"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 

     <class> 
      nl.han.dea.domain.Song 
     </class> 
     <class> 
      nl.han.dea.domain.Playlist 
     </class> 
     <class> 
      nl.han.dea.domain.Video 
     </class> 
     <class> 
      nl.han.dea.domain.User 
     </class> 

     <properties> 
      <property name="hibernate.archive.autodetection" value="class"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/> 
      <property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/> 
      <property name="hibernate.connection.url" 
         value="jdbc:mysql://localhost:3306/Spotitube?autoReconnect=true&amp;useSSL=false&amp;serverTimezone=UTC"/> 
      <property name="hibernate.connection.username" value="spotitube"/> 
      <property name="hibernate.connection.password" value="java"/> 
      <property name="hibernate.show_sql" value="true"/> 
      <property name="javax.persistence.sql-load-script-source" value="insert.sql"/> 
      <property name="hibernate.flushMode" value="FLUSH_AUTO"/> 
      <property name="hibernate.hbm2ddl.auto" value="update"/> 
     </properties> 

    </persistence-unit> 
</persistence> 
+0

beans.xml的是在Java EE 7中是可選的。將javaee-api的作用域更改爲提供。 – fg78nc

回答

1

你只需要在WEB-INF添加的beans.xml/

還要確保JavaEE的-API已經規定的範圍,確保排除從休​​眠-EntityManager的傳遞依賴JPA和JTA API,以確保你的戰爭具有正確的依賴關係,並且與EE服務器沒有衝突的API。

邊注:使用org.apache.tomee:JavaEE的-API:7.0-1:提供的,而不是使用javax:JavaEE的-API還可以使嵌入式測試工作(OpenEJB的或tomee嵌入式)

+0

最終的問題是IntelliJ生成的beans.xml像這個' CreasolDev