I am developing a new application in JavaEE 7 using WildFly 8. I am using JAX-RS to provide a RESTful service interface for remote applications.
Something like an HttpHeaders
object can be injected in a resource method arguments using the @Context
annotation. Since the object is based on request parameters (of course, the HTTP headers), I came up with the idea of creating my own injectable User
object which is created based on the presence of a valid token in the request (something like an OAuth access token).
So, I want to achieve something like this:
@Path("/resources")
public class MyResource {
@Path("/{id}")
@GET
public Response getById(@Context User user, @PathParam("id") long id) {
...
}
}
Where User is an injectable object created based on request parameters, such as ones accessible through an HttpHeaders
object. Of course, the provider can also throw an exception and return an HTTP error response if the User object cannot be created for any reason.
Now, my questions are:
Thanks
Open Liberty's JAX-RS 2.0 implementation uses Jackson as its default JSON provider.
Both Restlet and Jersey are two of the most popular implementation of JAX-RS used for developing RESTful web services in Java ecosystem but there is a couple of other implementation also exist like Apache Wink, Apache CXF, and JBoss RESTEasy, along with omnipresent and the most popular option of creating REST web ...
JAX-RS is a Java API for developing REST applications quickly. While JAX-RS provides a faster way for developing web applications than servlets, the primary goal of JAX-RS is to build RESTful services. JAX-RS 1.0 defines a server-side component API to build REST applications.
In my eyes this approach is valid as long as you don't try to build something like a session with this User Object.
As answered here you could use @Context and @Provider but that's not exactly what you want.
Directly injecting a Class per @Context
is possible with the Resteasy Dispatcher.
But here you must register the Object which should be injected. I don't think that makes sense for request-scoped parameters.
What you could do is inject a provider like this:
// Constructor of your JAX-RS Application
public RestApplication(@Context Dispatcher dispatcher) {
dispatcher.getDefaultContextObjects().put(UserProvider.class, new UserProvider());
}
// a resource
public Response getById(@Context UserProvider userProvider) {
User user = userProvider.get();
}
Other ways to solve your problem:
I pushed examples to github.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With