资源未从 web.xml 注入数据源 - Java 框架 - Payara Micro - NullPointerException

Resource not injecting Datasource from web.xml - Java Framework - Payara Micro - NullPointerException

我的 Payara REST API 在没有数据库连接的情况下工作正常,并且 Postgres 本地主机连接也通过 DriverManager 工作正常,即:

Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "pass@123")

但是,web.xml 和资源中 postgresql 的数据源在以下代码中抛出 NullPointerException:

public class AppService {
    @Resource(name="java:global/esWf")
    DataSource ds;
    
    public Response test ( 
    ...
    Connection conn = ds.getConnection();
    ...

WEB-INF/web.xml: -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
     version="3.1">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <display-name>PG-DataSource</display-name>
    <data-source>
     <name>java:global/esWf</name>
     <class-name>org.postgresql.ds.PGSimpleDataSource</class-name>
     <url>jdbc:postgresql://localhost:5432/postgres</url>
     <user>postgres</user>
     <password>pass@123</password>
     <property>
         <name>fish.payara.slow-query-threshold-in-seconds</name>
         <value>5</value>
     </property>
    </data-source>
</web-app>

我也尝试用服务器、端口、数据库名称的单独元素替换 url!

这是我第一次尝试通过数据源和资源进行连接,可能遗漏了一些小问题!

引用的链接:- https://blog.payara.fish/setting-up-a-data-source-in-payara-microhttps://dzone.com/articles/easy-java-ee-microservices-with-payara-micro, Payara 数据库示例等

错误:-

java.lang.NullPointerException
    at es.dms.DmsSrv.test(DmsSrv.java:65)
    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.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static[=15=](ResourceMethodInvocationHandlerFactory.java:52)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:124)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
    at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:176)
    at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
    at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
    at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:253)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:248)
    at org.glassfish.jersey.internal.Errors.call(Errors.java:244)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:680)
    at org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:394)
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:366)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:319)
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1636)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:259)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:757)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:577)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:158)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:520)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:217)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:182)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:156)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:218)
    at org.glassfish.grizzly.filterchain.ExecutorResolver.execute(ExecutorResolver.java:95)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:260)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:177)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:109)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:88)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:53)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:524)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:89)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:94)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access0(WorkerThreadIOStrategy.java:33)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:114)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:569)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:549)
    at java.lang.Thread.run(Thread.java:748)

这看起来与 resource injection in cdi bean 中的问题相同。

使用 @Resource 的注入在普通 CDI bean 和 JAX-RS 资源中不起作用。它仅适用于企业 bean。

您有 2 个选择:

  • 将您的 bean 变成无状态 EJB - 只需将 @Stateless 注释添加到 class:
@Stateless
@RequestScoped
@Path("app")
public class AppService {
    @Resource(name="java:global/esWf")
    DataSource ds;
    
    public Response test ( 
  • 创建 CDI 生产者,然后将数据源作为 CDI bean 注入。最佳做法是使用限定符来标记要注入哪个 DS(以防应用中有多个数据源):
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface Eswf {
}
---------------
@RequestScoped
public DsProducer {
  @Produces
  @Resource(name="java:global/esWf")
  @Eswf
  Datasource eswfDatasource;
}
------------------
@RequestScoped
@Path("app")
public class AppService {
    @Inject
    @Eswf
    DataSource ds;
    
    public Response test ( 

P.S。我认为您还需要更改 JNDI 名称以使用 java:app/ 命名空间而不是 java:global/,因为 DS 应该仅对已部署的应用程序可用。您可以测试是否可以使用 `new InitialContext().lookup("java:global/esWf") 通过 JNDI 名称检索 DS。这应该适用于您的应用程序的任何地方,而不仅仅是 EJB。