Monday 21, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Wednesday, 1 January 2014

JAX-RX @POST using RESTful web service

In this tutorial we are going to see about RESTful web service’s  @POST annotation.
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project as given below.
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder
add jar
4. Create package called com.javatutorialscorner.jaxrs.post under RESTful-WebService
5. Create Java class RESTfulPost under com.javatutorialscorner.jaxrs.post package
RESTfulPost.java
package com.javatutorialscorner.jaxrs.post;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;


@Path("restfulpost")
public class RESTfulPost {

@POST
@Path("post")
@Consumes(MediaType.APPLICATION_JSON)
public Response setStudentDetail(Student student){
String response = "Data Received Successfuly : "+student.getName();
return Response.status(201).entity(response).build();
}
}

6. Create Java class Student under com.javatutorialscorner.jaxrs.post package

Student.java

package com.javatutorialscorner.jaxrs.post;

public class Student {

String name;
int id;
String department;
int year;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getDepartment() {
return department;
}

public void setDepartment(String department) {
this.department = department;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

@Override
public String toString(){
return "{name : "+name+" , id : "+ id+" , department: "+department+",year : "+year+"}";
}
}

7.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JAX-RS-Path</display-name>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.javatutorialscorner.jaxrs.post</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL

<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>

In this web.xml above lines are additionally added for Jersey to Support JSON-Object mapping.

you can call the service by using from client class.

Create Client

Create package called com.javatutorialscorner.jaxrs.client under RESTful-WebService project

Create Client Java Class RESTfulClient under  com.javatutorialscorner.jaxrs.client package

RESTfulClient .java

package com.javatutorialscorner.jaxrs.client;

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.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class RESTfulClient {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String baseURI = "http://localhost:8080/RESTful-WebService/rest/restfulpost/post";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(baseURI);
String json = "{\"name\":\"Ram\",\"id\":33,\"department\":\"EEE\",\"year\":2011}";
ClientResponse response = service.type("application/json").post(
ClientResponse.class, json);
System.out.println("output for JSON Service ");

if (response.getStatus() != 201) {
throw new Exception("Error code : " + response.getStatus());
}

System.out.println("output " + response.getEntity(String.class));
} catch (Exception e) {
e.printStackTrace();
}
}

}

Now you can run the client and see the following output in console

output for POST Service

output Data Received Successfuly : Ram

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • File Upload using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to upload file using Jersey RESTful web service 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-WebServic… Read More
  • RESTful Web service with RESTEasy Client In this tutorials we are going to see the how to access the RESTful web service using  RESTEasy client. Here i am going to create two clients to access GET and POST request GET Request Refer this example for creatin… Read More
  • RESTful Web service with Apache HttpClient In this tutorials we are going to see the how to access the RESTful web service using Apache HttpClient. Here i am going to create two clients to access GET and POST request Add httpcore-4.1.jar, httpclient-4.1.1.jar, comm… Read More
  • Download Excel file using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to download text file using RESTful web service @Produces("application/vnd.ms-excel") annotation 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Proje… Read More
  • JAX-RS RESTful Web service to consume JSON and Produce XMLIn this tutorial we are going to see how to consume JSON and Produce XML using RESTful web service1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-W… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JAX-RX @POST using RESTful web service Rating: 5 Reviewed By: eHowToNow