Home  >  Web Services

@QueryParam Example in REST Web Service

By Arvind Rai, May 21, 2014
On this page we will provide @QueryParam Example in REST Web Service using RESTEasy. In JAXRS, to consume values from query string we use @QueryParam. It is applied in method argument level. For more than one keys in query string, we need to use more than one @QueryParam. Suppose query string is like

?name=Ram&collegeName=UP College

Then we can use @QueryParam as below

(@QueryParam("name") String name,@QueryParam("collegeName") String collegeName)

To access value of query string, use name and collegeName variable. Find the example to use @QueryParam
StudentService.java
package com.concretepage.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

@Path("/restwb")
public class StudentService {
	@GET
	@Path("/data")
	@Produces("application/json")
	public Student getStudentDetails(@QueryParam("name") String name,
			@QueryParam("collegeName") String collegeName) {
		Student student = new Student();
		student.setName(name);
		student.setCollegeName(collegeName);
		return student;
	}
} 
Method will return JSON response for the @QueryParam input. Use the URL as below.

http://localhost:8080/RestWB-1/restwb/data?name=Ram&collegeName=UP College

Find the Student class used in above web service.
Student.java
package com.concretepage.rest;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "student")
public class Student {
	private String name;
	private String collegeName;
	@XmlElement
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@XmlElement
	public String getCollegeName() {
		return collegeName;
	}
	public void setCollegeName(String collegeName) {
		this.collegeName = collegeName;
	}
} 
Find the output.
@QueryParam Example in REST Web Service

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us