Monday, 14 May 2018


JAX-RS Example Jersey
There are created 4 files for factorial JAX-RS example:
The first 3 files are created for server side and 1 application for client side.
//JAX-RS Server Code-----Dynamic Project(secondrestserver)
//1.RequestHandler.java
//2.web.xml
//3.index.jsp
//Required Jar File
asm-3.1.jar
jaxb-api-2.1.jar
jersey-bundle-1.2.jar
jersey-core-1.2.jar
jersey-server-1.14.jar
jersey-servlet-1.12.jar
jsr311-api-1.1.1.jar


===========================================================================
//JAX-RS Client Code- Simple Java Project(secondrestclient)
//4.ClientGovind.java
asm-3.1.jar
jaxb-api-2.1.jar
jersey-client-1.8.jar
jersey-core-1.8.jar


===========================================================================
//JAX-RS Server Code
Required Jar File
asm-3.1.jar
jaxb-api-2.1.jar
jersey-bundle-1.2.jar
jersey-core-1.2.jar
jersey-server-1.14.jar
jersey-servlet-1.12.jar
jsr311-api-1.1.1.jar
//RequestHandler.java
package govind;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
@Path("/rh")
public class RequestHandler {
                @POST
                @Path("fc")
                public String factorialCalculate(String no)
                {
                                long f=1;
                                int x=Integer.parseInt(no);
                                for(int i=1;i<=x;i++)
                                {
                                                f=f*i;
                                }
                                return "  "+f;
                }
}


//http://localhost:6789/secondrestserver/services/rh/fc
======================================================================
//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/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>8RestServer</display-name>
  <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
  <servlet>
<servlet-name>ab</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ab</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>

================================================================================
//index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
server ready..............
</body>
</html>

================================================================================

 //JAX-RS Client Code
//ClientGovind.java
package govind;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class ClientGovind {
               
                public static void main(String[] args) {
                                try
                                {
                                Client client = Client.create();
                                WebResource wr = client.resource("http://localhost:6789/secondrestserver/services/rh/fc");
                                ClientResponse cres = wr.accept("application/text").post(ClientResponse.class,"5");
                                String s1=cres.getEntity(String.class);
                                System.out.println("Factorial="+s1);
                                       }
               
                catch (Exception e) {
                                e.printStackTrace();
                                }
}
}

===============================================================================


o/p:--------------------

Factorial= 120

No comments:

Post a Comment