Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

JAX-RS: Developing RESTful Web Services in Java : Page 3

Learn how the JAX-RS API and annotations enable you to develop REST-based web services in Java.


advertisement

URI Template

As discussed earlier, you identify a root resource class using the @Path annotation. The value of the annotation can have a relative URI path template between the curly braces {,}, with the deployment context providing the reference to the base URI. A URI path template acts as a placeholder for a relative path URI. It generally is a string with zero or more embedded parameters in it, and it forms a valid URI path when values are applied for the parameters. Here is an example:

package com.demo; import javax.ws.rs.Path; @Path("/sayHello/{username}") public class SayHello { ...... ...... ...... }



In this example, the SayHello resource class is identified by the relative URI path sayHello/abc where abc is the value of the username parameter. However, { and } will not appear in the URI, as they are not valid. They are used only to specify the URI template.

You can retrieve the value of the username parameter by using the @PathParam annotation as the parameter value in part of the URI path. URI template parameters can optionally have a regular expression as well.

package com.demo; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @Path("/sayHello/{username}") public class SayHello { public SayHello() { } @GET ...... public String hello(@PathParam("username") String userName) { return " Hello, "+userName +" +Welcome to the world of JAX-RS! "; } }

Similarly, the parameter annotation @FormParam will be helpful to get the information from the form elements in the request. package com.demo;

import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.FormParam; @Path("/sayHello") public class SayHello { public SayHello() { } @POST ...... public String hello(@FormParam("username") String userName) { return " Hello, "+userName +" +Welcome to the world of JAX-RS! "; } }

Sub Resources

Apart from the resource class, you can also annotate methods of a resource class with the @Path annotation. This annotation specifies that the methods will be called as sub resource methods or sub resource locators.

Sub Resource Methods

Methods in a resource class that are annotated with @Path along with a request method designator annotation such as @GET, @POST, etc. are called as sub resource methods. Sub resource methods will be invoked for a URI request that is created by concatenating the URI template of the method with the URI template of the resource class.

package com.demo; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.FormParam; @Path("/sayHello") public class SayHello { public SayHello() { } @GET @Path("lastname") public String hello() { .............. } }

In the above example, a GET request from the URI /sayHello/lastname will be handled directly by the hello() sub-resource method in the SayHello resource class.

Sub Resource Locators

Methods in a resource class that are annotated with @Path, which dynamically identifies the object that will handle the request, are called sub resource locators. The return value for these methods is generally an object of a resource class that will handle the request. Sub resource locators are similar to normal resource methods, but they cannot have an entity parameter.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap