Convert JSON to java object in RESTful Web Services in Java

This example shows how to convert data from json to java object in RESTful Web Services with Java Using Jersey(JAX-RS). There are many ways for parse json string to object java in restful web services. In this java json parser example , we will use jackson dependency(maven project) if you are not use maven project then you can use jackson jar for JSON parsing.In this article we explain restful web services post method example in java  . We will send JSON data in post request and get JSON data from post request in java .

To learn how to convert json to java object in restful web service , firstly we will see example of restful web services , Now you have idea about rest api implementation in java , So in this example we will send request with json and parse it to java pojo . Firstly create a maven project as we have create in  simple restful web service example and will create handler class , configure web.xml and add dependencies in pom.xml . Now we will see implementation jax-rs post json example .

Java Restful API POST JSON Example

Step 1: Create hander class and define path , method and response. In this class we will write jax-rs post json method .

UserHander.java

package com.javaproficiency.jerseydemo.demo;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import com.javaproficiency.jerseydemo.model.User;

@Path("/user")
public class UserHander {

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response addUser(User user) {
System.out.println("user name"+user.getName());
System.out.println("user city="+user.getCity());
return Response.status(Status.OK).entity(user).build();
}

}

Step 2: In this example we need one java pojo for mapping json to java object . This java pojo have the same filed as key in json string . In our example we will use json with name and city key o string type , So we will create pojo with name and city field .

User.java

package com.javaproficiency.jerseydemo.model;

public class User {

private String name;
private String city;

public String getName() {
return name;
}

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

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

}

Step 3: configure web.xml file as

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>JerseyDemo</display-name>
<servlet>
<servlet-name>RestServices</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.javaproficiency.jerseydemo.demo</param-value>
</init-param>

<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestServices</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

Step 4: Add dependency in pom.xml file as bellow.

pom.xml

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>InstagramPost</groupId>
<artifactId>InstagramPost</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>com.craterzone.instagrampost</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.13</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older
 than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.13</version>
</dependency>

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<scope>provided</scope>
<version>2.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.10.1</version>
</dependency>

<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.2</version>
</dependency>

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-simple-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.16</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>2.16</version>
</dependency>

</dependencies>

</project>

Step 5:  This is  project structure of our application.

 

Step 6: Hit web service using rest console or postman

url =http://localhost:8080/JerseyDemo/rest/user

json:

{
“name”: “raja”,
“city”: “delhi”
}

 

Response:
{
“name”: “raja”,
“city”: “delhi”
}

we have seen example for convert json to pojo in java using jackson . We have seen implementation of jersey post json example in java .You have learned How to send JSON request to RESTful webservice in Java in this rest api post json example . You can learn more example of restful webservice using jersey in java .

One comment

  1. đồng tâm
    game mu
    cho thuê nhà trọ
    cho thuê phòng trọ
    nhac san cuc manh
    số điện thoại tư vấn pháp luật miễn phí
    văn phòng luật
    tổng đài tư vấn pháp luật
    dịch vụ thành lập công ty trọn gói
    64 nước cờ trên bàn thương lượng ebook
    mbp là gì
    thuyết erg
    các nghịch lý nổi tiếng
    chi square test là gì
    nghệ thuật nói chuyện trước công chúng dale carnegie
    định lý coase
    học thuyết kỳ vọng của victor vroom
    chiến thắng con quỷ trong bạn audio
    điểm cân bằng nash

    – Thái ca, lên thành phố thì em đúng là muốn, nhưng em nghĩ lên khu Khai Phát thành phố làm gì cơ? Người trong khu Khai Phát thành phố ai không có chỗ dựa, không chừng một cơn sóng là nuốt chết em.

    Triệu Quốc Đống nhìn ra ngoài cửa sổ rồi nói:
    – Em lên thành phố nhưng không phải bây giờ. Giang Khẩu mặc dù hơi nhỏ nhưng thích hợp với em. Có Thái ca làm chỗ dựa thì em không có gì phải lo ở Giang Khẩu. Em làm tốt công việc ở khu Khai Phát sẽ tạo được trụ cột cho mình.

    Thái Chánh Dương thở dài một tiếng, Triệu Quốc Đống này nói cũng đúng. Người có chút chức quan ở khu Khai Phát thành phố thì ai không có chỗ dựa? Triệu Quốc Đống còn trẻ như vậy, đến đó chỉ làm chân chạy mà thôi. Như vậy không bằng ở lại khu Khai Phát Giang Khẩu phấn đấu một phen, nếu có thể tạo chút thành tích thì điều lên thành phố cũng dễ.
    – Thái ca không nên tiếc như vậy, không phải có câu vàng ở đâu cũng sáng sao? Bây giờ để em lóe sáng ở Giang Khẩu đi, thời cơ đến thì sẽ lóe sáng ở Thành phố An Đô.
    Triệu Quốc Đống cười ha hả nói:
    – nhưng bây giờ Thái ca đã vào Thị ủy thì có tính toán gì không?

    – Tính toán gì chứ? Vào Thị ủy thì anh vẫn phụ trách mảng đó. Nếu khu Khai Phát thành phố không chuyển thành Khu công nghệ cao thì anh đúng là bị mắng to. Đúng là số khổ.
    Thái Chánh Dương thở dài nói.

    – Nhưng em thấy Thái ca hình như không ngại mệt.

    – Nói linh tinh, đến bước này chẳng lẽ nói anh làm mệt nên muốn nghỉ ư? Phải làm được thành tích mới không làm thất vọng vị trí của mình.

    – Công nghiệp, giao thông và khu Khai Phát chính là đầu tàu kinh tế của Thành phố An Đô, trọng trách của Thái ca rất nặng đó. Bí thư Ninh Pháp tin anh như vậy, Thái ca phải làm ra gì đó thì mới bịt miệng đám người không phục kia. Ngoại việc cổ phần hóa một số công ty hiệu quả kinh doanh kém, anh còn dự định gì không?
    Triệu Quốc Đống gật đầu nói.

Leave a Reply

Your email address will not be published. Required fields are marked *

+ 44 = 45