javacdijava-ee-7injecttomee-7

TomEE CDI @Inject NullPointerException


I'm trying to get CDI to work in my application, but I only get NullPointerExceptions when I do.

When normally instantiating the playlistService, it works perfectly fine, but when I try to use CDI, it doesn't anymore.

The server is able to start, but when I add beans.xml to META-INF/ and WEB-INF/ it's also not able to start anymore.

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>

EDIT:
resources/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>

Solution

  • you just need to add a beans.xml in WEB-INF/

    Also ensure javaee-api has the scope provided, ensure to exclude jpa and jta api from hibernate-entitymanager transitive dependencies to ensure your war has the right dependencies and no conflicting API with the EE server.

    Side note: using org.apache.tomee:javaee-api:7.0-1:provided instead of javax:javaee-api can also make embedded tests working (openejb or tomee-embedded)