RESTful Web Services annotation example @Path, @PathParam , @QueryParam, @MatrixParam, @FormParam

@Path example

On calling URI: “/persons” result: getPerson is called
On calling URI: “/persons/vip” result: getPersonVIP is called
		import javax.ws.rs.GET;
		import javax.ws.rs.Path;
		import javax.ws.rs.core.Response;

		@Path("/persons")
		public class PersonRestService {

			@GET
			public Response getPerson() {
				return Response.status(200).entity("getPerson is called").build();
			} 

			@GET
			@Path("/vip")
			public Response getPersonVIP() {
				return Response.status(200).entity("getPersonVIP is called").build();
			}
		}

	

@PathParam example

On calling URI: “/persons/1” result: getPersonById is called, id : 1
		import javax.ws.rs.GET;
		import javax.ws.rs.Path;
		import javax.ws.rs.PathParam;
		import javax.ws.rs.core.Response;

		@Path("/persons")
		public class PersonRestService {

			@GET
			@Path("{id}")
			public Response getPersonById(@PathParam("id") String id) {
				return Response.status(200).entity("getPersonById is called, id : " + id).build();
			}
		}	
	

@QueryParam example

On calling URI: “/persons/query?from=10&to=20&orderBy=age&orderBy=name”
result: getPersons is called, from : 10, to : 20, orderBy[age, name]
		import java.util.List;
		import javax.ws.rs.GET;
		import javax.ws.rs.Path;
		import javax.ws.rs.QueryParam;
		import javax.ws.rs.core.Response;

		@Path("/persons")
		public class PersonService {

			@GET
			@Path("/query")
			public Response getPersons(@QueryParam("from") int from, @QueryParam("to") int to,
			@QueryParam("orderBy") List<String> orderBy) {

			return Response.status(200).entity("getPersons is called, from : " + from + ", to : " + to
				+ ", orderBy" + orderBy.toString()).build();
			}
		}	
	

@MatrixParam example

On calling URI: “/books/2015” result: getBooks is called, year : 2015, author : null
On calling URI: “/books/2015;author= yogesh” result: getBooks is called, year : 2015, author : yogesh
		import javax.ws.rs.GET;
		import javax.ws.rs.MatrixParam;
		import javax.ws.rs.Path;
		import javax.ws.rs.PathParam;
		import javax.ws.rs.core.Response;

		@Path("/books")
		public class BookService {

			@GET
			@Path("{year}")
			public Response getBooks(@PathParam("year") String year, @MatrixParam("author") String author) {

				return Response.status(200).entity("getBooks is called, year : " + year + ", author : " + author).build();

			}
		}

	

@FormParam example

		import javax.ws.rs.FormParam;
		import javax.ws.rs.POST;
		import javax.ws.rs.Path;
		import javax.ws.rs.core.Response;

		@Path("/persons")
		public class PersonService {

			@POST
			@Path("/add")
			public Response addPerson(@FormParam("name") String name, @FormParam("age") int age) {

				return Response.status(200).entity("addPerson is called, name : " + name + ", age : " + age).build();

			}
		}
	
HTML form is as below
	
		
			

Name :

Age :