JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to read multiple values of a query paramter in JAX-RS restful web services?


In the previous example we have seen how to read query paramters from the request using @QueryParam annotation. What if the user request is in the below format? same id with multiple values?

http://localhost:8080/RestfulWebServices/employee/query?id=1016&id=1017&id=1415

In the above URL, the id value is same, but with different values, this can be easily readable as a list of values using UriInfo object.

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 which shows how to read multiple values with a unique request parameters:

package com.java2novice.restful;

import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

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

	@GET
	@Path("/query")
	public Response getEmployeeQuery(@Context UriInfo uriInfo){
		
		/** read complete employee id list from request query parameter**/
		List<String> empIdList = uriInfo.getQueryParameters().get("id");
		System.out.println("Received List: "+empIdList);
		/** read first employee id from request query parameter **/
		String firstEmpId = uriInfo.getQueryParameters().getFirst("id");
		System.out.println("First emp record from the request: "+firstEmpId);
		
		return Response.status(200).entity("processed request").build();
	}
}

In the above example, if you use "/employee/query?id=1016&id=1017&id=1415" URI pattern with query parameters, here is the output:


Output:
INFO  [stdout] (http--127.0.0.1-8080-1) Received List: [1016, 1017, 1415]
INFO  [stdout] (http--127.0.0.1-8080-1) First emp record from the request: 1016
<< 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
Interface and its usage
Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for declaring methods that one or more classes are expected to implement, capturing similarities between unrelated classes without forcing a class relationship and determining an object's programming interface without revealing the actual body of the class.
Famous Quotations
Millions long for immortality who do not know what to do with themselves on a rainy Sunday afternoon.
-- Susan Erz

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.