JAX-RS uses the @Path annotation for dispatching requests to corresponding resource or sub-resource methods using the following steps:
- Identify a set of candidate root resource classes matching the request.
- Obtain a set of candidate resource methods for the request.
- Identify the method that will handle the request.
- 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
|
|
HTTP GET request /s1/x/g2
|
|
HTTP GET request /s1/x/g1
|
|
HTTP GET request /s2/g1
|
|
HTTP PUT request /s2/g1 |
|
HTTP PUT request /s1/g1 |
|