Module 2: Jersey Welcome Example - dineshmadhup/RESTful-Web-Service GitHub Wiki
Package:
org.dinesh.softarica.productService.resources
Java File in above Package:
ProductResource.java
package org.dinesh.softarica.productService.resources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/products")
public class ProductResource {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getProducts() {
return "Welcome Products";
}
}
Note: There are three annotations used in project named “Welcome Products:
@Path(“/products")
@GET
@Produces(MediaType.TEXT_PLAIN)
How it works:
Before you analyze, look at web.xml file.
<web-app..>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.dinesh.softarica.productService</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
-
First of all tag is used inside . In we have two tags: and
-
The package inside is scanned and find the class called productService which contains one method - getProducts.
-
Finally getProducts method return the String “Welcome Products”.
To see the output, the url will be: