blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

JAX-RS: URIs and the Path annotations


The @Path annotation identifies the URI path template to which the resource responds and is specified at the class or method level of a resource.

URI path templates are URIs with variables embedded within the URI syntax. These variables are substituted at runtime in order for a resource to respond to a request based on the substituted URI. Variables are denoted by braces ({ and }).
eg:
@Path("/users/{username}")

A @Path value isn’t required to have leading or trailing slashes (/). The JAX-RS runtime parses URI path templates the same whether or not they have leading or trailing spaces.

By default, the URI variable must match the regular expression "[^/]+?"
This variable may be customized by specifying a different regular expression after the variable name.
eg:
 @Path("users/{username: [a-zA-Z][a-zA-Z_0-9]*}")

A URI path template has one or more variables, with each variable name surrounded by braces: { to begin the variable name and } to end it.
eg:
@Path("/{name1}/{name2}/")

You consume and use these parameters, add a @javax.ws.rs.PathParam annotation in either the resource constructor or the resource method.

Sample using @PathParam,
@Path("/hello/{username}")
public class HelloResource {
 @GET
 public String helloGet(@javax.ws.rs.PathParam("username") String username) {
  return "[GET] Hello "+username+" from JAX-RS on WebSphere Application server";
 }
}

Client code,
String lsUrl = "http://localhost:9080/RSServer/HelloApp/hello/John";
RestClient client = new RestClient();
Resource resource = client.resource(lsUrl);
ClientResponse result = resource.contentType(MediaType.TEXT_PLAIN).get();
System.out.println(result.getEntity(String.class));

Sample using @MatrixParam,
@Path("/hello")
public class HelloResource {
@GET
public String helloGet(@javax.ws.rs.MatrixParam("username") String username) {
return "[GET] Hello "+username+" from JAX-RS on WebSphere Application server";
}
}

Client code,
String lsUrl = "http://localhost:9080/RSServer/HelloApp/hello;username=John";
RestClient client = new RestClient();
Resource resource = client.resource(lsUrl);
ClientResponse result = resource.contentType(MediaType.TEXT_PLAIN).get();
System.out.println(result.getEntity(String.class));

Sample using @QueryParam,
@Path("/hello")
public class HelloResource {
@GET
public String helloGet(@javax.ws.rs.QueryParam("username") String username) {
return "[GET] Hello "+username+" from JAX-RS on WebSphere Application server";
}
}

Client code,
String lsUrl = "http://localhost:9080/RSServer/HelloApp/hello?username=John";
RestClient client = new RestClient();
Resource resource = client.resource(lsUrl);
ClientResponse result = resource.contentType(MediaType.TEXT_PLAIN).get();
System.out.println(result.getEntity(String.class));
(or)
String lsUrl = "http://localhost:9080/RSServer/HelloApp/hello";
RestClient client = new RestClient();
Resource resource = client.resource(lsUrl);
ClientResponse result = resource.contentType(MediaType.TEXT_PLAIN).queryParam("username", "John").get();
System.out.println(result.getEntity(String.class));

@DefaultValue annotation is used to specify a default value.
eg:
@DefaultValue("unknown") @javax.ws.rs.QueryParam("username") 

Both @QueryParam and @PathParam can be used only on the following Java types:


  • All primitive types except char
  • All wrapper classes of primitive types except Character
  • Any class with a constructor that accepts a single String argument
  • Any class with the static method named valueOf(String) that accepts a single String argument
  • List, Set, or SortedSet, where T matches the already listed criteria. Sometimes, parameters may contain more than one value for the same name. If this is the case, these types may be used to obtain all values

No comments:

Post a Comment