java - Jersey jdbc @resource not working -
i'm trying have simple jersey based jaxrs listener, running on existing tomcat 7.0.57. tomcat has global config in context.xml jdbc datasource, want use.
my problem can't resource resolve via @resource annotation.
heres 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 following
a: null b: null c: null d: null 1: org.apache.tomcat.jdbc.pool.datasource@1518c95{connectionpool[d.....
so jdbc connection configured, , can accessed explicit dolookup, why can't working @resource annotation?
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>
i've spend time searching i'm doing wrong can't find it. i've read posts suggesting adding things following web.xml snippets, 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>
for completeness, maven dependencies jersey:
<dependency> <groupid>org.glassfish.jersey.containers</groupid> <artifactid>jersey-container-servlet</artifactid> <version>2.19</version> </dependency>
it looks peeskillet right needing level of cdi.
i'll explain had add, kind of jumps away lot of instructions found.
first, need cdi manager, weld 1 of need dependency in our pom that
<dependency> <groupid>org.jboss.weld.servlet</groupid> <artifactid>weld-servlet</artifactid> <version>2.2.15.final</version> </dependency>
it need logging dependency, avoid warnings or logging, logs going normal java util logging
<dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-jdk14</artifactid> <version>1.7.12</version> </dependency>
only isn't enough, jersey apparently uses hk, not cdi need adapter.
<dependency> <groupid>org.glassfish.jersey.containers.glassfish</groupid> <artifactid>jersey-gf-cdi</artifactid> <version>2.6</version> </dependency>
then adapter seems have broken dependency need add 1 more avoid 2 page stack trace each request.
<dependency> <groupid>javax.transaction</groupid> <artifactid>javax.transaction-api</artifactid> <version>1.2</version> </dependency>
so have code in place, need 1 last thing make work.
you need empty web-inf/beans.xml. don't quite follow why though.
some sites state need meta-inf/context.xml entry, didn't find needed that, perhaps injecting custom classes?
hopefully helps else
Comments
Post a Comment