JAX-RS annotations

JAX-RS is an annotation-driven model for creating the web services. The annotations are applied on the classes to expose them as resources or RESTful APIs. Here, there are different annotations, such as @PATH , @GET ,@POST , @PUT, and @DELETE. These annotations correspond to HTTP verbs and CRUD operations in general. The classes will be annotated with @PATH annotation to represent the root of the resource, and each function will be annotated in correspondence with the HTTP verbs in order to respond to the HTTP requests. There are also the @QueryParam and @PathParam annotations, which are intended for data retrieval, and the @Produces and @Consumes  annotations for binding and unbinding data to the classes.

Furthermore, there are annotations that we can use for validation, such as @Email and @NotNull.

Let's take a look at these annotations in detail:

  • @Path: When a class is annotated with @Path , that class becomes a resource. This means it can be accessible using a URI from outside. Let's say we have a class called Controller. We use the @Path annotation on this Controller class to mark it as a resource as follows:
@Path("/home")
public class Controller{
}

This resource can now be accessed using a URI such as https://{{hostname}}/home.

Now that we have defined a resource, we need to add a function that responds to the request. These functions can be annotated with @GET, @POST, @PUT, or @DELETE, and so on, based on the action that the resource needs to perform. These annotations correspond to the HTTP verbs that we discussed and have the same meaning.

  • @GET: The @GET annotation is used for reading a resource. When @GET is applied to a function, a resource can be read on it. Consider the following example:
@Path("/home")
public class Controller{
@GET
public String login(){
return "login.html"
}
}

Similarly, we can define functions with @POST, @DELETE, and @PUT as follows:

 @POST
public String create(){
//..
}
@PUT
public String update(){
//..
}
@DELETE
public void delete(){
//..
}

Once we define the resources and functions, we also need to specify the MIME type through which data is exchanged in the specified format. Different MIME types that we can use include application/xml, application/json, image/png, and text/plain.

To specify the MIME type, we use the @Produces and @Consumes annotations.

  • @Produces: This is used to specify what type of data is returned to the client who is invoking the function via an HTTP request, as shown by the following code:
@Produces("application/json")
@POST
public String create(){
//..
}
  • @Consumes: This is used to specify what type of data is acceptable for the function that processes the HTTP request, as shown by the following code:
@Consumes("application/json")
@PUT
public String create(){
//..
}
  • @PathParam: This is used to map the query parameter to the parameter in a function for processing the request further. Now, @PathParam is usually used to retrieve the data by providing the input. This input is what gets mapped to the parameter of the function, which will then be used to pull the required data from the data source that the web service is tied to, as follows:
@Produces("application/json")
@GET
public getPerson(@PathParam("loginId") String loginId) {
}

Now let's take a look at how JAX-RS is actually implemented in an application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset