泽西岛jdbc @resource无法正常工作 [英] Jersey jdbc @resource not working

查看:59
本文介绍了泽西岛jdbc @resource无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我现有的tomcat 7.0.57上运行一个基于Jersey的简单jaxrs侦听器. Tomcat的context.xml中有一个我要使用的jdbc数据源的全局配置.

I'm trying to have a simple Jersey based jaxrs listener, running on my existing tomcat 7.0.57. Tomcat has a global config in its context.xml for a jdbc datasource, which I want to use.

我的问题是我无法通过@Resource批注获取要解析的资源.

My problem is that I can't get the resource to resolve via the @Resource annotation.

这里有一个简单的测试示例

Heres a simple test example

@Path("/")
public class TestJersey {
    @Resource(name = "jdbc/default")
    private DataSource dsA;

    @Resource(name = "java:comp/env/jdbc/default")
    private DataSource dsB;

    @Resource(lookup = "java:comp/env/jdbc/default")
    private DataSource dsC;

    @Resource(mappedName="jdbc/default")
    private DataSource dsD;

    @GET
    @Produces("text/plain")
    public String test() throws NamingException {
        StringBuffer ret = new StringBuffer();
        ret.append("A: " + dsA + "\n");
        ret.append("B: " + dsB + "\n");
        ret.append("C: " + dsC + "\n");
        ret.append("D: " + dsD + "\n");        
        DataSource ds1 = 
              (DataSource) InitialContext.doLookup("java:comp/env/jdbc/default");
        ret.append("1: " + ds1 + "\n");
        return ret.toString();
    }
}

此测试应用返回以下内容

This test app returns the following

A: null
B: null
C: null
D: null
1: org.apache.tomcat.jdbc.pool.DataSource@1518c95{ConnectionPool[d.....

因此,已配置了jdbc连接,并且可以使用显式的doLookup访问jdbc连接,那么为什么不能使用@Resource批注来使其连接呢?

So the jdbc connection is configured, and can be accessed with an explicit doLookup, so why can't I get it working with a @Resource annotation?

我的应用程序web.xml包含

My apps web.xml contains

<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.test</param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

我花了一些时间来寻找我做错了什么,但找不到.我读过一些文章,建议添加类似以下web.xml片段的内容,但它们没有帮助

I've spend some time searching for what I'm doing wrong but I can't find it. I've read posts suggesting adding things like the following web.xml snippets, but they haven't helped

  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/default</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

为了完整起见,我的Maven依赖项只是球衣:

For completeness, my maven dependencies are simply jersey:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.19</version>
</dependency>

推荐答案

peeskillet CDI水平.

我将解释我必须添加的内容,因为它与我发现的许多说明有所不同.

I'll explain what I had to add, as it kind of jumps away from a lot of the instructions I found.

首先,您需要一个CDI管理器,

First, you need a CDI manager, weld is one of those so we need a dependency in our pom for that

<dependency>
    <groupId>org.jboss.weld.servlet</groupId>
    <artifactId>weld-servlet</artifactId>
    <version>2.2.15.Final</version>
</dependency>

它还需要一个日志记录依赖项,以避免发出警告或获取任何日志记录,因此类似的操作将使日志记录进入普通的Java util日志记录

It will also need a logging dependency, to avoid warnings or get any logging, so something like this will get the logs going to normal java util logging

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.12</version>
</dependency>

仅那还不够,因为球衣显然使用hk,而不是cdi ,因此您需要一个适配器.

Only that isn't enough, as jersey apparently uses hk, not cdi so you need an adapter.

<dependency>
    <groupId>org.glassfish.jersey.containers.glassfish</groupId>
    <artifactId>jersey-gf-cdi</artifactId>
    <version>2.6</version>
</dependency>

然后,该适配器似乎具有断开的依赖关系,因此我们需要再添加一个以避免两个页面的堆栈跟踪每个请求.

Then that adapter seems to have a broken dependency so we need to add one more to avoid a two page stack trace each request.

<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>javax.transaction-api</artifactId>
    <version>1.2</version>
</dependency>

因此,现在您已具备所有代码,您需要做最后一件事以使其真正起作用.

So now you have all the code in place, you need to do one last thing to make it actually work.

您需要一个空的 WEB-INF/beans.xml .我不太明白为什么.

You need an empty WEB-INF/beans.xml. I don't quite follow why though.

一些站点还声明您需要一个META-INF/context.xml条目,但是我没有发现我需要它,也许是为了注入自定义类?

Some sites also state that you need a META-INF/context.xml entry, but I didn't find I needed that, perhaps it is for injecting custom classes?

希望这对其他人有帮助

这篇关于泽西岛jdbc @resource无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆