Java Libraries
Use JAR files which are listed below:
antlr-2.7.7.jar
asm-3.1.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
genson-1.3.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.5.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jackson-core-asl-1.9.2.jar
jackson-jaxrs-1.9.2.jar
jackson-mapper-asl-1.9.2.jar
jackson-xc-1.9.2.jar
javassist-3.15.0-GA.jar
javax.servlet.jsp.jstl-1.2.2.jar
javax.servlet.jsp.jstl-api-1.2.1.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
jersey-client-1.18.jar
jersey-core-1.18.jar
jersey-json-1.19.3.jar
jersey-server-1.18.jar
jersey-servlet-1.18.jar
jettison-1.1.jar
jsr311-api-1.1.1.jar
mysql-connector-java-5.1.36.jar
taglibs-standard-impl-1.2.1.jar
Create Server Project
Create Dynamic Web Project in Eclipse. Copy all jar files above to the lib directory in the project
Create ApplicationConfig Class
This class is used to register a URL pattern in Jersey to intercept HTTP calls to the service. The marshaller, Jersey intercepts the path given in @ApplicationPath annotation such as – @ApplicationPath(“api”). This class extends javax.ws.rs.core.Application. All the RESTful web services and extensions needed are registered here.
ApplicationConfig.java
package ws;
import java.util.Set;
import javax.ws.rs.core.Application;
@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(controllers.DemoRestController.class);
}
}
Create Restful Web Services
Create Restful Web Services have delete method use remove data
package controllers;
import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
@Path("demo")
public class DemoRestController {
@DELETE
@Path("delete/{id}")
public Response delete(@PathParam("id") String id) {
try {
System.out.println("Id is deleted: " + id);
return Response.ok().build();
} catch (Exception e) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
}
}
Consume Restful Web Services from Java Application
Create Java Project in Eclipse. Add all jar files above to the project
Create DemoRestClientModel
DemoRestClientModel class contain methods call restful web services
package models;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.DefaultClientConfig;
public class DemoRestClientModel {
private String BASE_URL = "http://localhost:8080/LearnJavaRestfulWebServices_Server/api/";
private WebResource webResource;
private Client client;
public DemoRestClientModel() {
this.client = Client.create(new DefaultClientConfig());
this.webResource = this.client.resource(BASE_URL).path("demo");
}
public ClientResponse delete(String id) {
ClientResponse response = null;
try {
WebResource resource = this.webResource;
response = resource.path("delete/" + id)
.delete(ClientResponse.class);
} catch (Exception e) {
response = null;
}
return response;
}
}
Run It
package demo;
import com.sun.jersey.api.client.ClientResponse;
import models.DemoRestClientModel;
public class Main {
public static void main(String[] args) {
DemoRestClientModel demoRestClientModel = new DemoRestClientModel();
ClientResponse response = demoRestClientModel.delete("p05");
if(response != null) {
int statusCode = response.getStatus();
System.out.println("Response Status: " + statusCode);
} else {
System.err.println("Can not access delete web method");
}
}
}
Output
Output from Client
Response Status: 200
Output from Server
Id is deleted: p05