Twitter

vineri, 2 octombrie 2015

JAX-RS working with @Path at class and method level examples

In this post, you can see a very simple JAX-RS application for working with @Path at class and method level

In the JAX-RS HelloWorld Example we have used @Path to define a relative URI at class level (this comes after the base URI). Basically, @Path can be used at:

·         class level:
@Path("/account")
public class HelloUsersResource {

 @GET   
 @Produces("text/plain")
 public String helloAdmin() {
  return "Hello, Admin!";
 }
   
 @GET   
 @Produces("text/plain")
 public String helloUser() {
  return "Hello, User!";
 }
}
A valid URL will be of type (the application name is, JaxrsUsingPathAnnotation_EE7, and the base URI is, webresources ):

http://localhost:8080/JaxrsUsingPathAnnotation_EE7/webresources/account

Is easy to intuit that the our relative URI looks ambiguous, because @Path is placed at class level and we have to methods that produces output. JAX-RS will pick up the helloAdmin() method for the above URL. But, if we rename the helloAdmin() as, helloXman(), then JAX-RS will pick up the helloUser() to produce the response for the above URL. It looks like, JAX-RS picked up the method based on kind of alphabetical criteria. Actually, this is the last criteria, while the most important are:

·         Isolate the methods that correspond to the HTTP request type. For example, if we simply paste our URL in the browser address bar, we will have a HTTP GET request. Further, if we annotate the helloAdmin() method with @POST (indicating a HTTP POST request), then JAX-RS will pick up the helloUser() method instead of helloAdmin():
@Path("/account")
public class HelloUsersResource {

 @POST
 @Produces("text/plain")
 public String helloAdmin() {
  return "Hello, Admin!";
 }
   
 @GET   
 @Produces("text/plain")
 public String helloUser() {
  return "Hello, User!";
 }
}
·         If a resource class is capable of producing more than one MIME media type then the resource method chosen will correspond to the most acceptable media type as declared by the client.  For example, we can use the @Produces at class level to indicate the MIME type of the expected response, and override it locally (at method level, e.g. helloAdmin()). In the below example, JAX-RS will pick up the helloUser() method as the most acceptable media type as declared by the client:
@Path("/account")
@Produces("text/html")
public class HelloUsersResource {

 @GET
 @Produces("text/plain")
 public String helloAdmin() {
  return "Hello, Admin!";
 }
   
 @GET   
 @Produces("text/html")
 public String helloUser() {
  return "Hello, User!";
 }
}
·         class and method level:

Using @Path at method level also will not override the @Path from class level. Actually, the relative URI will be obtained be concatenating the value of @Path from class level with the value if @Path from method level, as below - now we can build custom URL per method:
@Path("/account")
public class HelloUsersResource {

 @GET
 @Path("/admin")
 @Produces("text/plain")
 public String helloAdmin() {
  return "Hello, Admin!";
 }
   
 @GET   
 @Path("/user")
 @Produces("text/plain")
 public String helloUser() {
  return "Hello, User!";
 }
}
And, the accepted URL:

http://localhost:8080/JaxrsUsingPathAnnotation_EE7/webresources/account/admin
http://localhost:8080/JaxrsUsingPathAnnotation_EE7/webresources/account/user

The complete application is available here.

A @Path value may or may not begin with a '/', it makes no difference.

Niciun comentariu:

Trimiteți un comentariu