Genauer gesagt, mein POJO User.java:< /p>
Code: Select all
@Entity (name="User")
@Table(name = "users")
public class User {
@Id @GeneratedValue
@Column(name="id")
private int id;
@Column(name="username")
private String userName;
@Column(name="password")
private String password;
@Column(name="email")
private String emailAddress;
public User(String userName, String password, String emailAddress) {
super();
this.userName = userName;
this.password = password;
this.emailAddress = emailAddress;
}
public User() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
Code: Select all
@Stateless
public class UserDao {
@PersistenceContext (unitName="librarysoftjpa")
private EntityManager entityManager;
public List getAllUsers(){
List allUsers=null;
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
CriteriaQuery criteria = criteriaBuilder.createQuery(User.class);
criteria.from(User.class);
TypedQueryquery = entityManager.createQuery(criteria);
allUsers = query.getResultList();
return allUsers;
}
}
Code: Select all
@RequestScoped
@Path("/users")
@Produces("application/json")
@Consumes("application/json")
public class UserWebService {
private UserDao userDao;
public UserWebService(){
this.userDao=new UserDao();
}
@GET
@Path("/all")
public List getAllUsers(){
Listusers=this.userDao.getAllUsers();
return users;
}
}
Code: Select all
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
java:/PostGreLibrarySoftDS
org.declercq.librarysoftbackend.models.Permission
org.declercq.librarysoftbackend.models.Role
org.declercq.librarysoftbackend.models.User
Wenn ich jedoch auf den Webservice zugreife, erhalte ich eine Nullpointer-Ausnahme für meinen Entitymanager:< /p>
Code: Select all
10:17:10,095 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 62) WFLYUT0021: Registered web context: /librarysoftbackend
10:17:10,174 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "librarysoftbackend.war" (runtime-name : "librarysoftbackend.war")
10:17:10,341 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
10:17:10,342 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
10:17:10,343 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 14178ms - Started 422 of 670 services (404 services are lazy, passive or on-demand)
10:17:18,898 ERROR [io.undertow.request] (default task-3) UT005023: Exception handling request to /librarysoftbackend/rest/users/all: org.jboss.resteasy.spi.UnhandledException: j a v a . l a n g . N u l l P o i n t e r E x c e p t i o n < b r / > a t o r g . j b o s s . r e s t e a s y . c o r e . E x c e p t i o n H a n d l e r . h a n d l e A p p l i c a t i o n E x c e p t i o n ( E x c e p t i o n H a n d l e r . j a v a : 7 7 ) < b r / > a t o r g . j b o s s . r e s t e a s y . c o r e . E x c e p t i o n H a n d l e r . h a n d l e E x c e p t i o n ( E x c e p t i o n H a n d l e r . j a v a : 2 2 0 ) < b r / > at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:175)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:418)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:209)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
at org.declercq.librarysoftbackend.dao.UserDao.getAllUsers(UserDao.java:64)
at org.declercq.librarysoftbackend.rest.UserWebService.getAllUsers(UserWebService.java:69)
at org.declercq.librarysoftbackend.rest.UserWebService$Proxy$_$$_WeldClientProxy.getAllUsers(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:402)
... 43 more
Code: Select all
Caused by: java.lang.NullPointerException
at org.declercq.librarysoftbackend.dao.UserDao.getAllUsers(UserDao.java:64)
Code: Select all
CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder();
Irgendeine Idee, was ich hier falsch mache?
UPDATE: Das hier als mögliches Duplikat verlinkte Thema hat absolut nichts damit zu tun. Mein Problem liegt im Container-verwalteten Persistenzkontext und dem Einfügen eines Entitymanagers in meine Anwendung, nicht darin, dass ich nicht weiß, wie man ein Objekt initialisiert.
UPDATE2: Ich habe meinen Code ein wenig geändert und Betrachten Sie mein UserDao als Bean, indem Sie die Anmerkung @Stateless hinzufügen, nachdem Sie diesen Beitrag von Adam Bien gelesen haben: http://www.adam-bien.com/roller/abien/e ... ce_jpa_for. Grundsätzlich heißt es, dass DAOs für JPA nicht mehr benötigt werden, EJBs sind besser. Allerdings immer noch NullpointerException am selben Ort. (habe es auch mit @Stateful versucht, gleiches Ergebnis).
UPDATE3:
Konfiguration meiner Datenquelle in standalone.xml von Wildfly:
Code: Select all
jdbc:postgresql://127.0.0.1:5432/librarysoft
postgres
test
test
org.postgresql.Driver
Gelöst.
Die Lösung bestand darin, @Stateless hinzuzufügen Notation zu meiner UserDao-Klasse hinzufügen und daraus eine Bean machen.
Als nächstes fügen Sie in UserWebService das Attribut hinzu:
Code: Select all
@EJB
private UserDao userDao;