Searching...
Friday, 19 July 2013

RestFul Webservices @QueryParam Example

12:27
JAX-RS Binding values to method parameters

To call any service we need to bind the request to the resource method, in order to bind this JAX-RS has provided few parameter types, the data will be taken from these parameters by using the annotaions. Whenever JAX-RS provider receives an http request, it finds an appropriate java method  that can service this request.
Here are the parameters provided by JAX-RS API
  1. Query parameters
  2. URI path parameters
  3. Header parameters
  4. Form parameters
  5. Cookie parameters
  6. Matrix parameters
  7. Bean Parameters
In this example we will see how to use all these parameters.

1. Create a maven project using the quick start archetype and add below dependencies in your pom.xml file
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.2.3.v20140905</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>
2. Create a simple service which uses all these params
package com.vinod.vinod_rest_examples;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path("/DataBinding")

public class DataBinding {

/**
*http://localhost:8080/DataBinding/queryParam?name=vinod
*
*@param name
*@return
*/
@GET
@Path("queryParam")
@Produces(MediaType.APPLICATION_JSON)
public String queryParam(@QueryParam("name") String name) {
return "Hello world " + name;
}

/**
*http://localhost:8080/DataBinding/vinod/pathParam
*
*@param name
*@return
*/
@GET
@Path("{name}/pathParam")
@Produces(MediaType.APPLICATION_JSON)
public String pathParam(@PathParam("name") String name) {
return "Hello world " + name;
}

/**
*http://localhost:8080/DataBinding/headerParam
*
*@param userAgent
*@return
*/
@Path("headerParam")
@GET
@Produces(MediaType.APPLICATION_JSON)

public String headerParam(@HeaderParam("user-agent") String userAgent) {
return "Hello world " + userAgent;
}

/**
*http://localhost:8080/DataBinding/matrixParam;name=vinod;address=
* bangalore
*
*@param name
*@param address
*@return
*/
@Path("matrixParam")
@GET
@Produces(MediaType.APPLICATION_JSON)

public String matrixParam(@MatrixParam("name") String name, @MatrixParam("address") String address) {
return "Hello world " + name + " " + address;
}

/**
*http://localhost:8080/DataBinding/formParam pass the name in the form
* request
*
*@param name
*@return
*/
@Path("formParam")
@POST
public String formParam(@FormParam("name") String name) {
return "Hello world " + name;
}
}
3. Create a java class to start jetty server
package com.vinod.vinod_rest_examples;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

/**
 * Hello world!
 *
 */
public class App {
public static void main(String[] args) {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");

Server jettyServer = new Server(8080);
jettyServer.setHandler(context);

ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);

// Setting pacakge name over here to load services
jerseyServlet.setInitParameter("jersey.config.server.provider.packages",
"com.vinod.vinod_rest_examples");
try {
jettyServer.start();
jettyServer.join();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
jettyServer.destroy();
}
}
}
4. Hit the urls for testing

http://localhost:8080/DataBinding/queryParam?name=vinod
http://localhost:8080/DataBinding/vinod/pathParam
http://localhost:8080/DataBinding/headerParam
http://localhost:8080/DataBinding/matrixParam;name=vinod;address=bangalore
http://localhost:8080/DataBinding/formParam pass the name in the form
 
Download this example!! https://github.com/kkvinodkumaran/myrepository/tree/master/vinod-rest-examples 

0 comments: