jax rs - Resteasy and Google Guice: how to use multiple @ApplicationPath and resource with @Injection? -
i created project test dependency injection offered google guice in jax-rs resources, using resteasy.
my intentions are:
- use multiple
@applicationpath
versions of api. in each class annotated@applicationpath
load set of classes specific version. - each resource have
@inject
(from google guice) in constructor inject services.
i created 2 classes annotated @applicationpath
: applicationv1rs
, applicationv2rs
. in both added same resources classes (userresource
, helloresource
), test.
my module configured this:
public class hellomodule implements module { public void configure(final binder binder) { binder.bind(igreeterservice.class).to(greeterservice.class); binder.bind(iuserservice.class).to(userservice.class); } }
when call http://localhost:9095/v1/hello/world
or http://localhost:9095/v2/hello/world
, receive same error:
java.lang.runtimeexception: resteasy003190: not find constructor class: org.jboss.resteasy.examples.guice.hello.helloresource
well, expected, not works. google guice not "smart" instantiate resource classes using construtor me.
but can't find way work. honest, i'm confuse how google guice, jetty , resteasy play each other in scenario.
if abandon idea of use @applicationpath
, resources work google guice configuring hellomodule
this:
public class hellomodule implements module { public void configure(final binder binder) { binder.bind(helloresource.class); binder.bind(igreeterservice.class).to(greeterservice.class); binder.bind(userresource.class); binder.bind(iuserservice.class).to(userservice.class); } }
but in case, i'm passing control register resources (helloresource
, userresource
) guice. it's not flexible me, can't setup multiple @applicationpath
.
so, i'm missing or not understanding?
i created project problemetic code. easy setup , test: https://github.com/dherik/resteasy-guice-hello/tree/so-question/readme.md
thanks!
when have getclasses
method in application tries create instance registered resources using default constructor missing in our resources class. 1 way create default constructor , inject dependencies through setter injection. , instead of overriding getclasses
in applicationv1rs
, applicationv2rs
override getsingletons
. since resources can singleton.
below changes made make work way want.
applicationv1rs.java
@applicationpath("v1") public class applicationv1rs extends application { private set<object> singletons = new hashset<object>(); public applicationv1rs(@context servletcontext servletcontext) { } @override public set<object> getsingletons() { injector injector = guice.createinjector(new hellomodule()); helloresource helloresource = injector.getinstance(helloresource.class); userresource userresource = injector.getinstance(userresource.class); singletons.add(helloresource); singletons.add(userresource); return singletons; } }
applicationv2rs.java
@applicationpath("v2") public class applicationv2rs extends application { private set<object> singletons = new hashset<object>(); public applicationv2rs(@context servletcontext servletcontext) { } @override public set<object> getsingletons() { injector injector = guice.createinjector(new hellomodule()); helloresource helloresource = injector.getinstance(helloresource.class); userresource userresource = injector.getinstance(userresource.class); singletons.add(helloresource); singletons.add(userresource); return singletons; } }
helloresource.java
@path("hello") public class helloresource { @inject private igreeterservice greeter; public helloresource() { } @get @path("{name}") public string hello(@pathparam("name") final string name) { return greeter.greet(name); } }
userresource.java
@path("user") public class userresource { @inject private iuserservice userservice; public userresource() { } @get @path("{name}") public string hello(@pathparam("name") final string name) { return userservice.getuser(name); } }
add @singleton
service classes.
hope helps.
i have pushed code forked repo. check out
Comments
Post a Comment