JAX-RS: Java API for RESTful web services
JAX-RS is the Java API for developing RESTful web services.
It is a POJO-based, HTTP centric API. The API provides a set of annotations and associated classes/interfaces that may be used with POJOs in order to expose them as Web resources.
Some commonly used terms:
- Resource Class: A Java class that uses JAX-RS annotations to implement a corresponding Web resource.
- Root Resource Class: A resource class annotated with @Path. Root resource classes provide the roots of the resource class tree and provide access to sub-resources.
- Request Method Designator: A runtime annotation annotated with @HttpMethod. Used to identify the HTTP request method to be handled by a resource method.
- Resource Method: A method of a resource class annotated with a request method designator that is used to handle requests on the corresponding resource.
- Sub-resource Locator: A method of a resource class that is used to locate sub-resources of the corresponding resource.
- Sub-resource Method: A method of a resource class that is used to handle requests on a sub-resource of the corresponding resource. It is different from a sub-resource locator in that the method is annotated with a request method designator.
Hello World - Lets Start It
The following is a simple Hello World web resource which is developed using the JAX-RS annotations:
@Path("/helloworld")
public class HelloWorldResource {
@GET
public String sayHello() {
return "Hello World, welcome to the REST land!!!";
}
}
In this sample, the HelloWorldResource class is annotated with the @Path annotation, and is the root resource class as defined above. The method sayHello is annotated with the request method designator @GET and is the resource method. If we assume that the application is deployed on a container and that its base URI is http://someserver/restdemo, a HTTP GET request http://someserver/restdemo/helloworld shall invoke the sayHello resource method defined in the HelloWorldResource above.