Request matching

JAX-RS uses the @Path annotation for dispatching requests to corresponding resource or sub-resource methods using the following steps:

  1. Identify a set of candidate root resource classes matching the request.
  2. Obtain a set of candidate resource methods for the request.
  1. Identify the method that will handle the request.
  2. If no matching resource method or sub-resource method can be found, then an appropriate error response is returned.

Now let us illustrate, for the following services, Service1 and Service2, how request dispatching happens when a client requests the target resources:

@Path("/s1")
public class Service1 {
/**
* Creates a new instance of Service1
*/
public Service1() {
}

@GET
@Path("{name : .+}")
@Produces(MediaType.APPLICATION_JSON)
public String get1(@PathParam("name") String name) {
System.out.println("Service1.get1 Invoked");
return "Service1.get1."+name;
}

@GET
@Path("{name : .+}/g2")
@Produces(MediaType.APPLICATION_JSON)
public String get2(@PathParam("name") String name) {
System.out.println("Service1.get2 Invoked");
return "Service1.get2."+name;
}
}

Let's see how it works with Service 2  here:

@Path("/{any : .*}")
public class Service2 {
/**
* Creates a new instance of Service2
*/
public Service2() {
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public String get1() {
System.out.println("Service2.get1 Invoked");
return "Service2.get1";
}

@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void put1(String content) {
System.out.println("Service2.put1 Invoked");
System.out.println(content);
}
}

Now let us look at how the different client requests get resolved to the corresponding target resource:

Client request

Request matching steps

HTTP GET request /s1/g1

 

  • Matching root resource class is Service1 as @Path(“/s1”) best matches the Service1 class.
  • Candidate resource methods is get1 as the @Path expression resolves to {name : .+}. The .+ is a regular expression that will match any stream of characters after /s1.
  • The method that will handle the request is get1 in this case.

HTTP GET request /s1/x/g2

 

  • Matching root resource class is Service1 as @Path(“/s1”) best matches the Service1 class.
  • Candidate resource methods is get2 as the @Path expression resolves to {name : .+}/g2. The .+ is a regular expression that will match any stream of characters after /s1 that ends with g2.
  • The method that will handle the request is get2 in this case.

HTTP GET request /s1/x/g1

 

  • Matching root resource class is Service1 as @Path(“/s1”) best matches the Service1 class.
  • Candidate resource methods is get1 as the @Path expression resolves to {name : .+}. The .+ is a regular expression that will match any stream of characters after /s1.
  • The method that will handle the request is get1 in this case.

HTTP GET request /s2/g1

 

  • Matching root resource class is Service2 as @Path(“{any : .*}”) best matches the Service2 class.
  • Candidate resource methods is get1, which is the only available GET method in this class.
  • The method that will handle the request is get1 in this case.

HTTP PUT request /s2/g1

  • Matching root resource class is Service2 as @Path(“{any : .*}”) best matches the Service2 class.
  • Candidate resource methods is put1, as that is the only available PUT method in this class.
  • The method that will handle the request is put1 in this case.

HTTP PUT request /s1/g1

  • Matching root resource class is Service1 as @Path(“/s1”) best matches the Service1 class.
  • Candidate resource methods is get1 as the @Path expression resolves to {name : .+}. The .+ is a regular expression that will match any stream of characters after /s1.
  • As get1 does not support PUT requests, the server responds saying HTTP Status 405 - Method Not Allowed.
..................Content has been hidden....................

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