Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beans not injected in JAX-RS subresource #7422

Closed
sdaschner opened this issue May 4, 2019 · 3 comments
Closed

Beans not injected in JAX-RS subresource #7422

sdaschner opened this issue May 4, 2019 · 3 comments

Comments

@sdaschner
Copy link

sdaschner commented May 4, 2019

A JAX-RS subresource that is created by resourceContext.getResource(MyResource.class) doesn't inject beans properly into the created subresource instance.

Reproducer:

@Path("a")
@Produces(MediaType.APPLICATION_JSON)
public class AResource {

    @Inject
    ABean aBean;

    @Context
    ResourceContext resourceContext;

    @GET
    public List<String> getFoos() {
        return aBean.getFoos();
    }

    @Path("b")
    public BResource bResource() {
        return resourceContext.getResource(BResource.class);
    }

}
@Produces(MediaType.APPLICATION_JSON)
public class BResource {

    @Inject
    ABean aBean;

    @GET
    public List<String> getFoos() {
        return aBean.getFoos();
    }

}
@Stateless
public class ABean {

    public List<String> getFoos() {
        return List.of("foo", "bar");
    }

}

Produces the expected result for .../a but accessing .../a/b fails a 500 and the following:

[WARNING ] Application {http://jaxrs_test.tmp.sebastian_daschner.com/}AResource has thrown exception, unwinding now
org.apache.cxf.interceptor.Fault
[WARNING ] Exception in handleFault on interceptor org.apache.cxf.jaxrs.interceptor.JAXRSDefaultFaultOutInterceptor@d2d079cc
org.apache.cxf.interceptor.Fault
[ERROR   ] An unexpected error occurred during error handling. No further error processing will occur.
org.apache.cxf.interceptor.Fault
[ERROR   ] SRVE0777E: Exception thrown by application class 'com.sebastian_daschner.tmp.jaxrs_test.BResource.getFoos:23'
java.lang.NullPointerException
	at com.sebastian_daschner.tmp.jaxrs_test.BResource.getFoos(BResource.java:23)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at com.ibm.ws.jaxrs20.server.LibertyJaxRsServerFactoryBean.performInvocation(LibertyJaxRsServerFactoryBean.java:652)
	at [internal classes]

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.ibm.ws.logging.internal.impl.IntrospectionLevelMember$2 (file:/opt/wlp/lib/com.ibm.ws.logging_1.0.25.jar) to field java.util.logging.Logger.emptyHandlers
WARNING: Please consider reporting this to the maintainers of com.ibm.ws.logging.internal.impl.IntrospectionLevelMember$2
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
[ERROR   ] SRVE0315E: An exception occurred: java.lang.Throwable: java.lang.NullPointerException
	at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:5026)
	at [internal classes]
Caused by: java.lang.NullPointerException
	at com.sebastian_daschner.tmp.jaxrs_test.BResource.getFoos(BResource.java:23)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at com.ibm.ws.jaxrs20.server.LibertyJaxRsServerFactoryBean.performInvocation(LibertyJaxRsServerFactoryBean.java:652)
	... 1 more

Tested with Open Liberty 19.0.0.2/wlp-1.0.25.cl190220190207-0300 on Eclipse OpenJ9 VM, version 11.0.1+13 (en_US)

Which looks a bit weird to me, since I'm 100% sure that this worked in previous Liberty versions...

Any ideas?

@WhiteCat22
Copy link
Member

Which version of OpenLiberty are you using? Most recent?

@andymc12
Copy link
Contributor

andymc12 commented May 6, 2019

@sdaschner I wouldn't expect this scenario to have worked in the past. Perhaps the app code has changed somewhat from when it used to work?

Take a look at my new tests in PR #7423 - it should work if you modify the code somewhat:

  • The sub resource class needs to be a CDI bean - so it would need a bean defining annotation at the class level (in my test, I used @RequestScoped, but any scope should work...)
  • In the root resource, instead of using resourceContext.getResource(BResource.class), try resourceContext.initResource(CDI.current().select(BResource.class).get()) -- this should ensure that both types of injection (CDI via @Inject and JAX-RS via @Context) occur in the sub resource.

Hope this helps, Andy

@WhiteCat22 WhiteCat22 removed their assignment May 6, 2019
@sdaschner
Copy link
Author

@andymc12 I see, yes, I was re-reading JSR 339 again, and you're right, nothing forces JAX-RS to inject these. Btw, I believe we should change that in a future JAX-RS spec version...

I'm still puzzled, since I was completely sure that this worked before (haven't changed anything in my repo), but that's a problem I have to figure out myself :-)

Thanks for the prompt help folks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants