user6032518
user6032518

Reputation:

What is a JAX-RS resource?

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?? ;-)

Upvotes: 7

Views: 6695

Answers (3)

Alan Evangelista
Alan Evangelista

Reputation: 3163

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)

Upvotes: 1

Veracity
Veracity

Reputation: 49

Resource methods are methods of a resource class annotated with a request method designator like @GET, @PUT, @POST, or @DELETE.

The resource is what ever it is contacting that is responding. The behavious of the resource is determined by the HTTP method to which it is responding.

Just think of URL and URI, Uniform Resource Locator, Uniform Resource Identifier... http://examaple.com/mystuff The resource is the mapping to the stuff...

Upvotes: 0

JAX-RS resource classes (see the «root resource classes» below) are used to represent RESTful resources. The references:

  1. Chapter 3 Resources

    Using JAX-RS a Web resource is implemented as a resource class and requests are handled by resource methods. This chapter describes resource classes and resource methods in detail.

    3.1 Resource Classes

    A resource class is a Java class that uses JAX-RS annotations to implement a corresponding Web resource. Resource classes are POJOs that have at least one method annotated with @Path or a request method designator.

    JSR 339: JAX-RS 2.0: The Java API for RESTful Web Services, Version 2.0 Final Release, page 11.

  2. Root resource classes are "plain old Java objects" (POJOs) that are either annotated with @Path or have at least one method annotated with @Path or a request method designator, such as @GET, @PUT, @POST, or @DELETE. Resource methods are methods of a resource class annotated with a request method designator. This section explains how to use JAX-RS to annotate Java classes to create RESTful web services.

    29.2 Creating a RESTful Root Resource Class - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7).

RESTful resource:

5.2.1.1 Resources and Resource Identifiers

The key abstraction of information in REST is a resource. Any information that can be named can be a resource: a document or image, a temporal service (e.g. "today's weather in Los Angeles"), a collection of other resources, a non-virtual object (e.g. a person), and so on. In other words, any concept that might be the target of an author's hypertext reference must fit within the definition of a resource. A resource is a conceptual mapping to a set of entities, not the entity that corresponds to the mapping at any particular point in time.

Fielding Dissertation: CHAPTER 5: Representational State Transfer (REST).

Upvotes: 2

Related Questions