Sei sulla pagina 1di 5

BootStraping option in jersey------------

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

class---
-------------------
package com.product.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Path("/product")
public class ProductResource {
@GET
@Produces(value=MediaType.TEXT_PLAIN)
public int getPrice(@QueryParam("productCode")String productCode) {
return hashCode();//we return has code for checking purpose to how
many obj are created at a time
//return 500;

#1
web.xm
----------------------------------------
<servlet>
<servlet-name>jersy</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-
class>

<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.product.resource</param-value>
</init-param>

<load-on-startup>1</load-on-startup>

</servlet>
<servlet-mapping>
<servlet-name>jersy</servlet-name>
<url-pattern>/product</url-pattern>
</servlet-mapping>

In this case multiple has code will be generated.

===================================================================================
==================================================
#2 write class name insted of packages and in param value pass the fully qualified
name of class.

In this case multiple has code will be generated.

<servlet>
<servlet-name>jersy</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-
class>

<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>com.product.resource.ProductResource</param-value>
<!-- fully qualified name of cls -->
</init-param>

<load-on-startup>1</load-on-startup>

</servlet>
<servlet-mapping>
<servlet-name>jersy</servlet-name>
<url-pattern>/product</url-pattern>
</servlet-mapping>

===================================================================================
===================================================
#3
If we have multiple resources as part of the application which we wanted to make
singletons and prototypes/request type.
Rec--60;40 second

first create one pakg and write one class nd class name should be projrect name(as
convention follow by the people because there are several resource contain one
project that way.)

like this 0---


package com.bootstrap.common;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

import com.product.resource.ProductResource;
@ApplicationPath("/product")
public class JerseyBootStarping extends Application{
private Set<Object> singletons;
//private Set<Class<?>> classes;

//write cons
public JerseyBootStarping() {
singletons=new HashSet<Object>();//class name we dont know here
so insted of cls we have to pass the object
singletons.add(new ProductResource());

@Override
public Set<Object> getSingletons() {
return singletons;
}
}

web.xml---
----------------
<servlet>
<servlet-name>jersy</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-
class>

<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.bootstrap.common.JerseyBootStarping</param-
value> <!-- fully qualified name of cls -->
</init-param>

<load-on-startup>1</load-on-startup>

</servlet>
<servlet-mapping>
<servlet-name>jersy</servlet-name>
<!-- <url-pattern>/product</url-pattern> -->
<url-pattern>/web/*</url-pattern>
</servlet-mapping>

In this case only one has code will be generated.


===================================================================================
===================================================
#4
If jee5 or servlet3.1 or if there is no web.xml then---
first remove the web.xml how?---simply put web.back..xml then it will
automatically unreadable.

@ApplicationPath("/product") ---->only write this.

public class JerseyBootStarping extends Application{


private Set<Object> singletons;
//private Set<Class<?>> classes;

//write cons
public JerseyBootStarping() {
singletons=new HashSet<Object>();//class name we dont know here
so insted of cls we have to pass the object
singletons.add(new ProductResource());

@Override
public Set<Object> getSingletons() {
return singletons;
}
===================================================================================
===================================================

#5

from own ward all are called the quick boot straping of jersey.

first go to custom class and comment the path=//@ApplicationPath("/product") like


this

web.xml--->pass directly the application name


------
<servlet>
<servlet-name>javax.ws.rs.core.Application</servlet-name>

</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/product</url-pattern>
<!-- <url-pattern>/web/*</url-pattern> -->
</servlet-mapping>

===================================================================================
=================================================
#6---
---------------------
pass the fully pkg with qualified name of created custtom class.

<servlet>
<servlet-name>com.bootstrap.common.JerseyBootStarping</servlet-name>

</servlet>
<servlet-mapping>
<servlet-name>com.bootstrap.common.JerseyBootStarping</servlet-name>
<url-pattern>/product</url-pattern>

</servlet-mapping>

===================================================================================
===============================================
#7
Mostely used in people in real time. v.v.i

First create one class name with config nd extend from ResourceConfig (Why config ?
bez- people will follow the naming convention.)

package com.bootstrap.config;

import org.glassfish.jersey.server.ResourceConfig;

import com.product.resource.ProductResource;

public class Config extends ResourceConfig{

//create one cons


public Config() {
super.register(new ProductResource());//call or class
// TODO Auto-generated constructor stub
}

web.xml=
<servlet>
<servlet-name>com.bootstrap.config.Config</servlet-name>

</servlet>
<servlet-mapping>
<servlet-name>com.bootstrap.config.Config</servlet-name>
<url-pattern>/product</url-pattern>
</servlet-mapping>
-----------------------------------------------------------------------------------
-----------------------------

Withot Web.xml-----
==================
First annotate with config class --->@ApplicationPath("/api/*") ndpass the fully
qname of config class.
package com.bootstrap.config;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;

import com.product.resource.ProductResource;

@ApplicationPath("/api/*")----------->write this
public class Config extends ResourceConfig{

//create one cons


public Config() {
super.register(new ProductResource());
// TODO Auto-generated constructor stub
}

Potrebbero piacerti anche