Saturday 21 December 2013

JAX-RS Returning Data Using Response

JAX-RS  Returning Data

Hi friends .i am bit busy now a days.as i have told you in this session i am going to demonstrate you how different content types will be returned by jax-rs.

in below example i am again demonstrating you the same example of university web service.before going into code i will show how web service works using schematic representation:






The code snippet and detail description is marked as a comment in code.the code is as follows:
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="get" action="rest/university/Json">

    <table>
            <tr>
                <td><input type="text" name="usename"></input>
                </td>
            </tr>
           
            <tr>
                <td><input type="password" 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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

university web service

package org.web;

import javax.json.Json;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("/university")
public class Univeristy
{
    @SuppressWarnings("unused")
    @Context
    private UriInfo context;

    /**
     * Default constructor.
     */
    public Univeristy()
    {
        // TODO Auto-generated constructor stub
    }
   
  //call by  http://localhost:8080/rest/university/Json after passing values to form

    @Path("/Json")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response displayJsondata(@FormParam("username") String username,@FormParam("password")String password)
    {
       
        if(username.equals("") && password.equals(""))
            return Response.serverError().entity("Username and password should not be blank").build();
       
        String  data=username+password;
       
        return Response.ok(data,MediaType.APPLICATION_JSON_TYPE).build();
       
       
    }
   
    //Response using form parameters
   //call by  http://localhost:8080/rest/university/Xml
       
    @Path("/Xml")
    @GET
    @Produces(MediaType.APPLICATION_ATOM_XML)
    public Response displayXmlData(@FormParam("username") String username,@FormParam("password")String password)
    {
        if(username.equals("") && password.equals(""))
        return Response.serverError().entity("Username and password should not be blank").build();
        String data=username+password;
       
        return Response.ok(data,MediaType.APPLICATION_ATOM_XML).build();
    }
   
   
  

}

Thanks hope you will get it.
For Any query related to post ping me on pathak.nisarg@yahoo.com

No comments:

Post a Comment

Spring Boot SSL configuration -Tomcat Server

Hi Friends hope you all are doing well. Today I am going to demonstrate about how to configure SSL in Spring boot web Application. Need o...