JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

JAX-RS @QueryParam and @DefaultValue annotations example


This page gives you an example using JAX-RS @QueryParam and @DefaultValue annotations, which injects value from request parameters to your method input parameters.

In the previous examples we have given details of application setup, dependencies, web.xml file configurations: If you want to know about these configuration, please refer these:

Restful web services using RESTEasy hello world example.
Restful web services using Jersey hello world example.

Look at the below class to apply @QueryParam annotation: You have two query parameters here: dept and branch.

package com.java2novice.restful;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/employee")
public class QueryParamExampleService {

	@GET
	@Path("/query")
	public Response getEmployeeQuery(@QueryParam("dept") String department,
									@QueryParam("branch") String branch){
		String resp = "Query parameters are received. 'dept' value is: "
						+department+" and branch value is: "+branch;
		
		return Response.status(200).entity(resp).build();
	}
}

In the above example, if you use "/employee/query?branch=hydrabad&dept=finance" URI pattern with query parameters, getEmployeeQuery() method will be invoked, and you will get "Query parameters are received. 'dept' value is: finance and branch value is: hydrabad" as a response.

How to assign default values to method input variables if the query parameters are not available? You can use @DefaultValue annotation to specify default value.

package com.java2novice.restful;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

@Path("/employee")
public class QueryParamExampleService {

	@GET
	@Path("/query")
	public Response getEmployeeQueryDefault(
					@DefaultValue("accounts") @QueryParam("dept") String department,
					@DefaultValue("bangalore") @QueryParam("branch") String branch){
		String resp = "Query parameters are received. 'dept' value is: "
						+department+" and branch value is: "+branch;
		
		return Response.status(200).entity(resp).build();
	}
}

In the above example, if you use "/employee/query" URI pattern, and you will get "Query parameters are received. 'dept' value is: accounts and branch value is: bangalore" as a response.

In the next page you will see an example on how to use @MatrixParam annotation.

<< Previous Program | Next Program >>
blog comments powered by Disqus

Restful Web Services Examples

  1. Restful web services using RESTEasy hello world example.
  2. Restful web services using Jersey hello world example.
  3. JAX-RS @Path annotation example
  4. JAX-RS @Path annotation with regular expression match example
  5. JAX-RS @PathParam annotation example
  6. JAX-RS @QueryParam and @DefaultValue annotations example
  7. JAX-RS @MatrixParam annotation example
  8. How to read multiple values of a query paramter in JAX-RS restful web services?
  9. How to pass header parameters as method inputs in JAX-RS restful web services?
  10. How to read header parameters in JAX-RS restful web services?
  11. JAX-RS @FormParam annotation example
  12. How to upload file using Jersey restful web services?
  13. How to download file using java restful web services?
  14. XML based Restful web service with RESTEasy and JAXB.
  15. XML based Restful web service with Jersey and JAXB.
  16. Json based Restful web service with RESTEasy, Jettison and JAXB
  17. Json based Restful web service with RESTEasy and Jackson
  18. Json based Restful web service with Jersey and Jackson
  19. How to input json request with Jersey and Jackson?
  20. Java client for restful web service using java.net package
  21. Java client for restful web service using Jersey API
  22. Java restful webservices with HTTP basic authentication.
Knowledge Centre
Can interface be final?
No. We can not instantiate interfaces, so in order to make interfaces useful we must create subclasses. The final keyword makes a class unable to be extended.
Famous Quotations
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.