Rest API simple Application with http mothods

We will be developing REST API using JAX-RS (Jersey) and Tomcat server and we will be implementing basic 4 methods so lets get started

Here is pom.xml file:

<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.dpq.webservices</groupId><artifactId>SimpleRestApiApp</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>SimpleRestApiApp</name><build>        <finalName>SimpleRestApiApp</finalName>        <plugins>                 <plugin>                         <groupId>org.apache.maven.plugins</groupId>                         <artifactId>maven-compiler-plugin</artifactId>                         <version>3.8.1</version>                         <inherited>true</inherited>                         <configuration>                                        <source>1.8</source>                                        <target>1.8</target>                         </configuration>                </plugin>        </plugins></build><dependencyManagement>                      <dependencies>                                    <dependency>                                                <groupId>org.glassfish.jersey</groupId>                                                <artifactId>jersey-bom</artifactId>                                                <version>${jersey.version}</version>                                                <type>pom</type>                                                <scope>import</scope>                                    </dependency>                      </dependencies></dependencyManagement><dependencies><dependency><groupId>org.glassfish.jersey.media</groupId><artifactId>jersey-media-json-jackson</artifactId><version>${jersey.version}</version></dependency> <dependency><groupId>jakarta.xml.bind</groupId><artifactId>jakarta.xml.bind-api</artifactId><version>3.0.0</version></dependency><dependency><groupId>com.sun.xml.bind</groupId><artifactId>jaxb-impl</artifactId><version>3.0.0</version><scope>runtime</scope></dependency><dependency><groupId>org.glassfish.jersey.media</groupId><artifactId>jersey-media-jaxb</artifactId><version>3.0.2</version></dependency><dependency><groupId>org.glassfish.jersey.containers</groupId><artifactId>jersey-container-servlet-core</artifactId></dependency><dependency><groupId>org.glassfish.jersey.inject</groupId><artifactId>jersey-hk2</artifactId></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>2.3.0</version></dependency>             </dependencies><properties><jersey.version>3.0.3</jersey.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>

Here Jersey ServletContainer will serve request with using specified url-pattern and will lookup resources to be serves inside mentioned package

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><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.dpq.webservices.resources</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>Jersey Web Application</servlet-name><url-pattern>/*</url-pattern></servlet-mapping></web-app>
package org.dpq.webservices.resources;import java.util.Arrays;import java.util.HashMap;import java.util.Map;import jakarta.inject.Singleton;import jakarta.ws.rs.Consumes;import jakarta.ws.rs.DELETE;import jakarta.ws.rs.GET;import jakarta.ws.rs.Path;import jakarta.ws.rs.PathParam;import jakarta.ws.rs.Produces;import jakarta.ws.rs.core.MediaType;@Path("/msgs")@Produces({MediaType.APPLICATION_JSON , MediaType.TEXT_PLAIN})@Consumes(MediaType.APPLICATION_JSON)@Singletonpublic class MyResource {  private Map<Long,String> messages = new HashMap<>();  public MyResource() {    System.out.println("Instance created...");    messages.put(1L,"I am fine");    messages.put(2L,"How are you");    messages.put(3L,"Hope you are doing well");  }  @GET  @Path("/{messageId}")  @Produces(MediaType.TEXT_PLAIN)  public String getMessage(@PathParam("messageId") long id) {    return messages.get(id);  }     @DELETE  @Path("/{messageId}")  public void deleteMessage(@PathParam("messageId") long id) {    messages.remove(id);    System.out.println("messages:: "+messages);  }  @GET  @Path("/all")  @Produces(MediaType.TEXT_PLAIN)  public String getAllMessages() {    return Arrays.toString(messages.values().stream().toArray(String [] :: new));  }  @GET  @Path("/dummy")  @Produces(MediaType.TEXT_PLAIN)  public String getText() {     return "Dummy message";  }}
Please follow my below Git hub for this repo

https://github.com/Deepak-Bhardwaj-Architect/SimpleRestApiApp

https://github.com/Deepak-Bhardwaj-Architect/SimpleRestApiApp

Popular posts from this blog

What is Garbage collection in Spark and its impact and resolution

How to change column name in Dataframe and selection of few columns in Dataframe using Pyspark with example

Window function in PySpark with Joins example using 2 Dataframes (inner join)