Parameter Passing using JAX-RS
Hello friends hope you all are fine.i am going to demonstrate you simple code for how to passing various kinds of parameters using jax-rs web service.some of the annotations which is used to pass parameter to web service is as follows:
@FormParam
->used to get form parameters passed to web service
@QueryParam
->used to get form parameters passed to web service
@PathParam
->used to get form parameters passed to web service
@MatrixParam
->used to get form parameters passed to web service .
Sample Code:
index.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:directive.page contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"/>
<jsp:output doctype-root-element="html"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
omit-xml-declaration="true" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Insert title here</title>
</head>
<body>
<form method="post" action="rest/university/param">
<table>
<tr>
<td><input type="text" name="usename"></input>
</td>
</tr>
<tr>
<td><input type="text" name="password"></input>
</td>
</tr>
<tr>
<td><input type="submit" name="submit"></input>
</td>
</tr>
</table>
</form>
</body>
</html>
</jsp:root>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>Jersey Rest Services</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Rest Services</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Sample jax-rs code :
package org.paramdemo;
import javax.websocket.server.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
@Path("/university")
public class University
{
@SuppressWarnings("unused")
@Context
private UriInfo context;
/**
* Default constructor.
*/
public University() {
// TODO Auto-generated constructor stub
}
/**
* Retrieves representation of an instance of University
* @return an instance of String
*/
@Path("/param")
@GET
@Produces("application/xml")
public String getFormData(@FormParam("username")String username ,@FormParam("password")String password)
{
return "Your UserName is : " +username +" & Password is : "+password;
}
@GET
@Path("/queryparam")
public Response getQueryData(@QueryParam("username") String username,@QueryParam("Password")String password)
{
String info="Username is: "+username+""+"password is: "+password;
return Response.status(200).entity(info).build();
}
@GET
@Path("/pathparam/{username}/{password}")
public Response getPathData(@PathParam("username") String Username,@PathParam("password")String Password)
{
String info="UserName is :"+Username+"Password is :"+Password;
return Response.status(200).entity(info).build();
}
@GET
@Path("/matrixparam")
public Response GetMatrixParam(@MatrixParam("username")String Username,@MatrixParam("password")String Password)
{
String info="UserName is :"+Username+"Password is :"+Password;
return Response.status(200).entity(info).build();
}
}
Hope you will get my point
Thanks
Testing a Restful web service is similar to testing a Soap web service.so i am not going to discuss it next time.
if you have any query regarding webservice than ping me.
next time i will write on how to how to generate json/xml document using jax-rs and how to integrate Spring ,hibernate & jsf into a single web application.
for any query ping me on pathak.nisarg@yahoo.com
Hello friends hope you all are fine.i am going to demonstrate you simple code for how to passing various kinds of parameters using jax-rs web service.some of the annotations which is used to pass parameter to web service is as follows:
@FormParam
->used to get form parameters passed to web service
@QueryParam
->used to get form parameters passed to web service
@PathParam
->used to get form parameters passed to web service
@MatrixParam
->used to get form parameters passed to web service .
Sample Code:
index.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
<jsp:directive.page contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"/>
<jsp:output doctype-root-element="html"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
omit-xml-declaration="true" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Insert title here</title>
</head>
<body>
<form method="post" action="rest/university/param">
<table>
<tr>
<td><input type="text" name="usename"></input>
</td>
</tr>
<tr>
<td><input type="text" name="password"></input>
</td>
</tr>
<tr>
<td><input type="submit" name="submit"></input>
</td>
</tr>
</table>
</form>
</body>
</html>
</jsp:root>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>Jersey Rest Services</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Rest Services</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Sample jax-rs code :
package org.paramdemo;
import javax.websocket.server.PathParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.MatrixParam;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
@Path("/university")
public class University
{
@SuppressWarnings("unused")
@Context
private UriInfo context;
/**
* Default constructor.
*/
public University() {
// TODO Auto-generated constructor stub
}
/**
* Retrieves representation of an instance of University
* @return an instance of String
*/
@Path("/param")
@GET
@Produces("application/xml")
public String getFormData(@FormParam("username")String username ,@FormParam("password")String password)
{
return "Your UserName is : " +username +" & Password is : "+password;
}
@GET
@Path("/queryparam")
public Response getQueryData(@QueryParam("username") String username,@QueryParam("Password")String password)
{
String info="Username is: "+username+""+"password is: "+password;
return Response.status(200).entity(info).build();
}
@GET
@Path("/pathparam/{username}/{password}")
public Response getPathData(@PathParam("username") String Username,@PathParam("password")String Password)
{
String info="UserName is :"+Username+"Password is :"+Password;
return Response.status(200).entity(info).build();
}
@GET
@Path("/matrixparam")
public Response GetMatrixParam(@MatrixParam("username")String Username,@MatrixParam("password")String Password)
{
String info="UserName is :"+Username+"Password is :"+Password;
return Response.status(200).entity(info).build();
}
}
Hope you will get my point
Thanks
Testing a Restful web service is similar to testing a Soap web service.so i am not going to discuss it next time.
if you have any query regarding webservice than ping me.
next time i will write on how to how to generate json/xml document using jax-rs and how to integrate Spring ,hibernate & jsf into a single web application.
for any query ping me on pathak.nisarg@yahoo.com
No comments:
Post a Comment