gpt4 book ai didi

java - TomEE CDI @Inject NullPointerException

转载 作者:行者123 更新时间:2023-12-02 12:56:23 27 4
gpt4 key购买 nike

我试图让 CDI 在我的应用程序中工作,但当我这样做时,我只会得到 NullPointerExceptions。

当正常实例化 playlistService 时,它​​工作得很好,但是当我尝试使用 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>

最佳答案

你只需要在WEB-INF/中添加一个beans.xml即可

还要确保 javaee-api 具有提供的范围,确保从 hibernate-entitymanager 传递依赖项中排除 jpa 和 jta api,以确保您的 war 具有正确的依赖项,并且与 EE 服务器没有冲突的 API。

旁注:使用 org.apache.tomee:javaee-api:7.0-1:provided 代替 javax:javaee-api 也可以使嵌入式测试正常工作(openejb 或 tomee-embedded)

关于java - TomEE CDI @Inject NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44411625/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com