JAX-RS @PathParam Example
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
- Query parameters
- URI path parameters
- Header parameters
- Form parameters
- Cookie parameters
- Matrix parameters
- Bean Parameters
In this example we will see how to use multiple path params for a single call
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.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("/statedetails")
public class PathParamExample {
// http://localhost:8080/statedetails/India/KL
@GET
@Path("{country}/{state}")
@Produces("application/xml")
public Response getMsg(@PathParam("country") String country, @PathParam("state") String state) {
String stateDetails = null;
if (country.equals("India") & state.equals("KL")) {
stateDetails = "<State><name>KERALA</name><shortname>KL</shortname>"
+ "<headq>TRIVANDRUM</headq><language>MALAYALAM</language></State>";
} else {
stateDetails = "Data not found";
}
return Response.ok().entity(stateDetails).build();
}
}
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("/statedetails")
public class PathParamExample {
// http://localhost:8080/statedetails/India/KL
@GET
@Path("{country}/{state}")
@Produces("application/xml")
public Response getMsg(@PathParam("country") String country, @PathParam("state") String state) {
String stateDetails = null;
if (country.equals("India") & state.equals("KL")) {
stateDetails = "<State><name>KERALA</name><shortname>KL</shortname>"
+ "<headq>TRIVANDRUM</headq><language>MALAYALAM</language></State>";
} else {
stateDetails = "Data not found";
}
return Response.ok().entity(stateDetails).build();
}
}
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
Download this example!! https://github.com/kkvinodkumaran/myrepository/tree/master/vinod-rest-examples 
No comments:
Post a Comment