Saturday, January 22, 2022

How to Start Jersey Api Framework






 1. How to Use 
Jersey Hibernate File

step 1: Open dilsecodie
step 2: Rest Api section goto jersey part 
step 3: copy hibernate code in hibernate.cfg.xml 
step 4: use for session factory connection from the database any database  like Mysql,SQL , oracle 
step 5: the table should be mapped in hibernate file as an entity class.



From Jersey Framework we can develop any restful API structure 
To start with Jersey project, add jersey dependencies to pom.xml file in any dynamic 
projects mainly jersey dependency depend on these dependencies you can add these in your project.  
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.19.4</version>
</dependency>
 <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-atom</artifactId>
    <version>1.19.4</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-fastinfoset</artifactId>
    <version>1.19.4</version>
</dependency>
 <dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.19.4</version>
</dependency>

Now can add some changes in web.xml file 

<servlet>
  <servlet-name>s1</servlet-name>
  <servlet-class>
 org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
  
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>resource</param-value>
  
  
  </init-param>
 <init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>

  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>s1</servlet-name>
  <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
  </welcome-file-list>


So when any request comes to action class it is 1st call servlet where the rest as URL,
then its search endpoint  for API where its map its end from scanning param value 
packages to get all URLs 

ServletContainer is a servlet class that loads all packages which have URLs endpoint.

To start with Jersey project, add jersey dependencies to pom.xml file in any dynamic 
projects mainly jersey dependency depend on these dependencies you can add these in your project.  
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.19.4</version>
</dependency>
 <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-atom</artifactId>
    <version>1.19.4</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-fastinfoset</artifactId>
    <version>1.19.4</version>
</dependency>
 <dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.19.4</version>
</dependency>

Now can add some changes in the web.xml file 

<servlet>
  <servlet-name>s1</servlet-name>
  <servlet-class>
 org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  <init-param>
  
  <param-name>jersey.config.server.provider.packages</param-name>
  <param-value>resource</param-value>
  
  
  </init-param>
 <init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>

  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>s1</servlet-name>
  <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
  </welcome-file-list>


So when any request comes to action class it is 1st call servlet where rest as URL ,
then its search endpoint  for API where its map its end from scanning param value 
packages to get all URLs 

ServletContainer is servlet class that loads all packages which have URLs endpoint 
  
These are the import packages required for the jersey action class

import javax.servlet.http.HttpServletRequest; - for request handler 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.apache.wink.json4j.OrderedJSONObject;
import org.hibernate.Session; - to start hibernate session 

@Path("/demoList") - its for end point for API .








 

No comments:

Post a Comment