In addition, in many use cases you may want to translate each incoming request into a CDI RequestScope. This requires some custom handling of each request, before passing it down to RestEasy for processing. This allows you to properly scope all of your objects, though you cannot use a session scoped object (since there would be no active session).
The code is pretty simple to do this. You can find details on my github repository: https://github.com/johnament/resteasy-netty-cdi
First, define your endpoint. In my test case, I added a very simple one:
Next, we need some code to initialize the server. I added this directly in my test, but I would imagine most people would want to initialize it elsewhere.
As you can see in the test, I am using a custom CdiNettyJaxrsServer, which is what enables me for CDI integration. The only thing different about mine versus the normal one is what RequestDispatcher I use. The RequestDispatcher is what RestEasy provides to handle the incoming requests and what the response looks like. It's very low level. I decided this was the exact point I wanted to start the CDI RequestScope. So my RequestDispatcher looks like this
So whenever a request comes in, I start the context (using Weld's BoundRequestContext) and on completion I end it. I also created a custom CdiInjectorFactory for Netty. This alleviates a bug in the base one that depends on a ServletContext being available (throughs a NullPointerException). It's just a simplified version of the injector factory
You'll also notice in my test code I'm using a CDI Extension - LoadPathsExtension. This simply sits on the classpath and listens as Weld initializes.
For each ProcessAnnotatedType it observes, it checks if Path is present. If Path is present, it adds it to a local list of all resources.
This makes scanning for Paths possible, which is done by the container for RestEasy. In the Netty deployment, you need to always maintain your list of resources.
Finally, we start the actual test which uses the JAX-RS client API to make a request to a specific resource.