What is a JAX-RS resource?

Viewed 4616

I keep hearing the word “resource” in reference to JAX-RS and I don’t know what it means. I know what a JAX-RS web service is. I’ve created them often enough but I find the word “resource” very ambiguous. There are quite a lot of things that JAX-RS is or can be, so every time I hear the world "resource", my brain interprets it as "magical mystery thingy that does, we know not what". In the most explicit terms possible, what is a JAX-RS resource? Is it a client? Server? Either? Something else all together? The word resource seems to get tossed around for everything and yet I can hardly ever find a Java object of type Resource in the code. Then there's Spring & ORM mapping annotations..

If I were a 911 operator and you called me to have yourself pieced back together after a terrible accident, I might then send you a “medical resource”. Is that a mechanic named Joe who dropped out of paramedic school and is riding a bike, or is it an actual paramedic in an ambulance? ;-)

Joe is scrubbing up for surgery. Any last words?? ;-)

3 Answers

The name "resource" comes from the definition of a REST resource, as mentioned in Sergey's answer. In practice, in Java code using a JAX-RS implementation, it usually refers to a resource class, a Java class with methods that handle REST HTTP API endpoints related to the same entity. The HTTP request method defines the intended operation on the entity. Examples of endpoints that handle song entities and would usually be implemented in methods of a single JAX-RS resource class:

  • /song, GET method (lists all songs)
  • /song, POST method (creates a song)
  • /song, DELETE method(deletes all songs)
  • /song/1, GET method (gets song 1 data)
  • /song/1, PUT method or /song/1, PATCH method (updates song 1)
  • /song/1, DELETE (deletes song 1)
Related