Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
=======================================
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>
===================================================================================
==================================================
#2 write class name insted of packages and in param value pass the fully qualified
name of class.
<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.)
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>
//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.
</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;
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{