JAX-RS is used to make Rest Api , in this tutorial we make a Hello Word Programe Using JAX-RS
For creating JAX-RS we have Use the Net Beans IDE and use the following steps:
1– Create a maven web project
( File -> New Project -> Maven -> Web Project)
2– Add This two Depenedency in your pom.xml
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.25</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.25</version>
</dependency>
NOTE: You can download this dependency from https://mvnrepository.com/ website
3– Edit your web.xml and write jersey configuration inside that
In this project we use the following annotations
@Path
@GET
@Path : @path annotation is used to describe the url for our application
Example : @Path(“firstJerseyPrograme”)
@GET : Get annotation describe that this is the get request this request is used only for read data
Now start coding and use the follow steps
1- Create Maven Web Project and Add The above Describe dependency in your pom.xml inside <dependencies></dependencies> tag
pom.xml
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.25</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.25</version>
</dependency>
</dependencies>
2- Edit your web.xml and add the following code inside that for create jersey endpoints
<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>com.vasu.jerseyrest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
NOTE : Please define <param-value> </param-value> value correctly it will be your package name.
3- Create Java Class inside the package that you define inside the <param-value> </param-value> tag, i have created the class with name HelloWord.java
HelloWord.java :
package com.vasu.jerseyrest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
@Path(“firstJerseyPrograme”)
public class HelloWord {
@GET
public String a(){
return “hello JavaDream”;
}
}
4- Now Run Your Project
( Right Click On Project -> Run )
5- Enter The Url In Browser
http://localhost:8080/JerseyRest/rest/firstJerseyPrograme
http://localhost:8080/ -> Your localhost
JerseyRest -> Your Project Name
rest -> value that you define between <url-pattern></url-pattern> tag you can define any name there and user the same name in url
firstJerseyPrograme -> it is the name of path that you define in your class @Path(“firstJerseyPrograme”)