1

I have a simple JAX-RS InvoiceResource with an injected bean:

@Path("invoices")
public class InvoiceResource {

    @Inject
    private InvoiceStore invoiceDao;

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public Response create(@Context UriInfo uri, InvoiceDto invoiceDto) {

I wanted to add validation of the InvoiceDto I'm recieving, so I added an annotation:

@POST
@Produces(MediaType.APPLICATION_JSON)
public Response create(@Context UriInfo uri, @Valid InvoiceDto invoiceDto) {

Now it seems that this breaks CDI. I start to get the following error:

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=InvoiceStore,parent=InvoiceResource,qualifiers={})

I was expecting a validation exception istead. What am I doing wrong? I'm using glassfish 4 web profile, and if I understood correctly applying validation to params is part of Java EE 7 CDI 1.1 spec.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
adrianmoya
  • 719
  • 1
  • 6
  • 17

2 Answers2

1

Its clear form error that its not Bean validation issue but its DI issue . The actual problem is that the Jersey JAX-RS implementation uses the HK2 DI framework, HK2 is not simply aware of CDI beans. You have to make CDI beans available for the HK2 injection bindings. you can follow this post

HK2 Glassfish Integration

HK2 can be used in GlassFish applications. Every deployed GlassFish application has
a unique ServiceLocator associated with it that can be looked up with 
  JNDI name java:app/hk2/ServiceLocator. 

Same issue posted here

Simple solution is that if you want to use weld CDI then you need to just put empty beans.xml file in web-inf.

Community
  • 1
  • 1
Asif Bhutto
  • 3,916
  • 1
  • 24
  • 21
  • Ok, so I had the beans.xml in the wrong place and think I've been using WELD all this time. Now, I moved it to WEB-INF. Now I still get the error, and did look at the other question you mentioned, but this seems like a hack for Glassfish. Will this be portable to other AppServer? What's the JEE7 standar way? – adrianmoya Nov 29 '13 at 19:28
  • To be clear: Jersey's implementation of JAX-RS uses its own DI Framework, and I need to manually glue the standard CDI to it. But shouldn't this be a task of the AppServer? What happens if I change to another AppServer which uses other JAX-RS implementation? – adrianmoya Nov 29 '13 at 19:36
  • for CDI beans.xml is necessary what ever you are using RI. either its weld or openwebbean – Asif Bhutto Nov 29 '13 at 21:11
1

This is a bug in Glassfish 4.0.0 implementation. I've found this article which describes what's happening and gives a workaround:

http://www.rogerdelafuente.com/glassfish-4-jax-rs-cdi-bean-validation-bug/

Thanks to everybody who stepped in, it helped me a lot to understand the HK2 thing. Finally it should work without any hacks, making the app portable, once this bug is fixed.

adrianmoya
  • 719
  • 1
  • 6
  • 17
  • oh its not a glass fish bug – Asif Bhutto Nov 29 '13 at 21:04
  • It is. Replacing the jar as the article said removed the problem. You can try with the example in the article. – adrianmoya Nov 30 '13 at 01:07
  • Mine is GlassFish Server Open Source Edition 4.0 (build 89), but I'm running it from maven with cargo, (cargo:run), which is downloading the appserver from http://download.java.net/glassfish/4.0/release/glassfish-4.0-web.zip – adrianmoya Nov 30 '13 at 01:53