java - Dependency injection with Jersey 2.0 -
starting scratch without previous jersey 1.x knowledge, i'm having hard time understanding how setup dependency injection in jersey 2.0 project.
i understand hk2 available in jersey 2.0, cannot seem find docs jersey 2.0 integration.
@managedbean @path("myresource") public class myresource { @inject myservice myservice; /** * method handling http requests. returned object sent * client "text/plain" media type. * * @return string returned text/plain response. */ @get @produces(mediatype.application_json) @path("/getit") public string getit() { return "got {" + myservice + "}"; } } @resource @managedbean public class myservice { void servicecall() { system.out.print("service calls"); } }
pom.xml
<properties> <jersey.version>2.0-rc1</jersey.version> <project.build.sourceencoding>utf-8</project.build.sourceencoding> </properties> <dependencymanagement> <dependencies> <dependency> <groupid>org.glassfish.jersey</groupid> <artifactid>jersey-bom</artifactid> <version>${jersey.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencymanagement> <dependencies> <dependency> <groupid>org.glassfish.jersey.core</groupid> <artifactid>jersey-common</artifactid> </dependency> <dependency> <groupid>org.glassfish.jersey.core</groupid> <artifactid>jersey-server</artifactid> </dependency> <dependency> <groupid>org.glassfish.jersey</groupid> <artifactid>jax-rs-ri</artifactid> </dependency> </dependencies>
i can container start , serve resource, add @inject myservice, framework throws exception:
severe: servlet.service() servlet [com.noip.myapplication] in context path [/jaxrs] threw exception [a multiexception has 3 exceptions. are: 1. org.glassfish.hk2.api.unsatisfieddependencyexception: there no object available injection @ injectee(requiredtype=myservice,parent=myresource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1039471128) 2. java.lang.illegalargumentexception: while attempting resolve dependencies of com.noip.myresource errors found 3. java.lang.illegalstateexception: unable perform operation: resolve on com.noip.myresource ] root cause org.glassfish.hk2.api.unsatisfieddependencyexception: there no object available injection @ injectee(requiredtype=myservice,parent=myresource,qualifiers={}),position=-1,optional=false,self=false,unqualified=null,1039471128) @ org.jvnet.hk2.internal.threethirtyresolver.resolve(threethirtyresolver.java:74)
starter project available @ github: https://github.com/donaldjarmstrong/jaxrs
you need define abstractbinder
, register in jax-rs application. binder specifies how dependency injection should create classes.
public class myapplicationbinder extends abstractbinder { @override protected void configure() { bind(myservice.class).to(myservice.class); } }
when @inject
detected on parameter or field of type myservice.class
instantiated using class myservice
. use binder, need registered jax-rs application. in web.xml
, define jax-rs application this:
<servlet> <servlet-name>myapplication</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <init-param> <param-name>javax.ws.rs.application</param-name> <param-value>com.mypackage.myapplication</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>myapplication</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
implement myapplication
class (specified above in init-param
).
public class myapplication extends resourceconfig { public myapplication() { register(new myapplicationbinder()); packages(true, "com.mypackage.rest"); } }
the binder specifying dependency injection registered in constructor of class, , tell application find rest resources (in case, myresource
) using packages()
method call.