i've been searching hours find solution problem can't work. want inject weld-managed service constraintvalidator used validate user-object posted jax-rs rest-service. deployed glassfish 4.1 server.
i have service
@applicationscoped public class userservice { } and want inject constraintvalidator this
public class uniqueusernamevalidator implements constraintvalidator<uniqueusername, apiuser> { @inject private userservice service; @override public void initialize(uniqueusername constraintannotation) { } @override public boolean isvalid(apiuser value, constraintvalidatorcontext context) { return service.getbyusername(value.getusername()) == null; } } the rest resource looks this
@path("users") @produces(mediatype.application_json) public class userresource { @inject userservice userservice; @post public response createuser(@valid apiuser apiuser) { apirepresentation created = userservice.create(apiuser); return response.created(createurl(created)).build(); } } when post json user object following exception:
org.glassfish.hk2.api.unsatisfieddependencyexception: there no object available injection @ systeminjecteeimpl(requiredtype=userservice,parent=uniqueusernamevalidator,qualifiers={},position=-1,optional=false,self=false,unqualified=null,173822971) @ org.jvnet.hk2.internal.threethirtyresolver.resolve(threethirtyresolver.java:74) @ org.jvnet.hk2.internal.utilities.justinject(utilities.java:947) @ org.jvnet.hk2.internal.servicelocatorimpl.inject(servicelocatorimpl.java:902) @ org.jvnet.hk2.internal.servicelocatorimpl.createandinitialize(servicelocatorimpl.java:977) @ org.jvnet.hk2.internal.servicelocatorimpl.createandinitialize(servicelocatorimpl.java:968) @ org.glassfish.jersey.internal.inject.injections.getorcreate(injections.java:173) i'm aware jersey uses hk2 di provider , constraintvalidator created using injectingconstraintvalidatorfactory in return uses resourcecontext. since hk2 doe know nothing weld container managed beans can not inject proper service when creating constraintvalidator.
to fix searching
a) way provide jax-rs (preferable pure jax-rs way without dependency jersey) custom constraintvalidatorfactory create validator.
or b) way force jersey use weld di provider or tell hk2 pickup container managed beans without manually adding every single bean hk2. have no idea how use bridge proposed here .
i appreciate help.
cheers
do have possibility change underlying jax-rs implementation project?
when had same problem, switched jersey resteasy (fully certified jax-rs implementation). http://resteasy.jboss.org/
changing implementation easy enough: include dependy through favorite build automation tool (i use gradle):
compile 'org.jboss.resteasy:resteasy-servlet-initializer:3.0.11.final' additionally, make cdi work, include resteasy-cdi jax-rs cdi bridge:
compile 'org.jboss.resteasy:resteasy-cdi:3.0.11. lastly if want same json format, include resteasy-jackson-provider:
compile 'org.jboss.resteasy:resteasy-jackson-provider:3.0.11.final' in end, switching resteasy gave me lot less headache trying implement jersey fix.
Comments
Post a Comment