user6454491
user6454491

Reputation: 158

What exactly are JAX-RS/REST resources?

"Resources are one of the fundamental concepts in REST. REST emphasizes the manipulation of resources rather than issuing function calls. Resources have unique identifiers. In HTTP terms, this means associating every resource with at least one URL." https://cwiki.apache.org/confluence/display/WINK/JAX-RS+Resources,+HTTP+Methods,+and+Paths

What do we implement as a resource? Is it a group of functionalities, an object, multiple objects..?

Upvotes: 3

Views: 80

Answers (2)

VoiceOfUnreason
VoiceOfUnreason

Reputation: 57194

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.

This abstract definition of a resource enables key features of the Web architecture. First, it provides generality by encompassing many sources of information without artificially distinguishing them by type or implementation. Second, it allows late binding of the reference to a representation, enabling content negotiation to take place based on characteristics of the request. Finally, it allows an author to reference the concept rather than some singular representation of that concept, thus removing the need to change all existing links whenever the representation changes (assuming the author used the right identifier).

Fielding

What do we implement as a resource? Is it a group of functionalities, an object, multiple objects..?

Pretty much anything that has one or more representations that can be serialized into a byte stream.

Most design guidelines for URI (uniform resource identifiers) call for using nouns, rather than verbs, justifying that with the argument that the resource is a logical entity.

Upvotes: 2

jan.supol
jan.supol

Reputation: 2805

See Section 3 of the JAXRS Specification :

Using JAX-RS a Web resource is implemented as a resource class and requests are handled by resource methods.

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.

So basically it is a class, usually with @Path annotation,

@Path("/")
public class HelloWorldResource {
 ...
}

Resource methods are methods of a resource class annotated with a request method designator.

And Resource method is a method, usually annotated with @GET, or @POST, or other:

@GET
public String getHello() {
   return "Hello";
}

A request method designator is a runtime annotation that is annotated with the @HttpMethod annotation. JAX-RS defines a set of request method designators for the common HTTP methods: @GET, @POST, @PUT, @DELETE, @HEAD and @OPTIONS. Users may define their own custom request method designators including alternate designators for the common HTTP methods.

For more details, see jersey documentation about resources.

Upvotes: 1

Related Questions