Search code examples
javajsonjerseyjax-rsjavabeans

Converting JSON Object to Java Object in Jersey JAX-RS


I'm trying to convert a json object to an java object in a rest api but I don't know how to create my java bean.

The json object itself contains multiple objects and arrays. What instance variable do i have to put in the java bean to match these? My first guess would be another beans but this sounds somewhat messy with a high nesting extent.

Here is a sample json for visualization:

{
  key1: value,
  key2: value,
  anotherJsonObject: {
    key3: value,
    key4: value,
    anotherJsonObject: {
      key5: value
      ...
    },
    anotherJsonArray: [
      {
        key6: value,
        key7: value
      },
      {
        key6: value,
        key7: value
      }
    ]
  }
}

Solution

  • Here is the complete example using JAX-RS

    So first let us define sample JSON.

    [{
    		"id": 1,
    		"firstName": "Jeanette",
    		"lastNname": "Penddreth",
    		"email": "[email protected]",
    		"gender": "Female",
    		"ipAddress": "26.58.193.2",
    		"websitesVisited": [{
    				"websiteName": "www.youtube.com",
    				"IpAddress": "26.58.193.6",
    				"timeSpent": "1 Hr",
    				"NoOfTimeVisitedInDay": "10"
    			},
    			{
    				"websiteName": "www.facebook.com",
    				"IpAddress": "26.58.193.10",
    				"timeSpent": "2 Hr",
    				"NoOfTimeVisitedInDay": "20"
    			}
    		]
    
    	}
    
    	, {
    		"id": 2,
    		"firstName": "Giavani",
    		"lastName": "Frediani",
    		"email": "[email protected]",
    		"gender": "Male",
    		"ipAddress": "229.179.4.212",
    		"websitesVisited": [{
    				"websiteName": "www.youtube.com",
    				"IpAddress": "26.58.193.6",
    				"timeSpent": "1 Hr",
    				"NoOfTimeVisitedInDay": "10"
    			},
    			{
    
    				"websiteName": "www.facebook.com",
    				"IpAddress": "26.58.193.10",
    				"timeSpent": "2 Hr",
    				"NoOfTimeVisitedInDay": "20"
    			}
    
    		]
    	}, {
    		"id": 3,
    		"firstName": "Noell",
    		"lastName": "Bea",
    		"email": "[email protected]",
    		"gender": "Female",
    		"ipAddress": "180.66.162.255",
    		"websitesVisited": [{
    				"websiteName": "www.youtube.com",
    				"IpAddress": "26.58.193.6",
    				"timeSpent": "1 Hr",
    				"NoOfTimeVisitedInDay": "10"
    			},
    			{
    				"websiteName": "www.facebook.com",
    				"IpAddress": "26.58.193.10",
    				"timeSpent": "2 Hr",
    				"NoOfTimeVisitedInDay": "20"
    			}
    
    		]
    	}, {
    		"id": 4,
    		"firstName": "Willard",
    		"lastName": "Valek",
    		"email": "[email protected]",
    		"gender": "Male",
    		"ipAddress": "67.76.188.26",
    		"websitesVisited": [{
    				"websiteName": "www.youtube.com",
    				"IpAddress": "26.58.193.6",
    				"timeSpent": "1 Hr",
    				"NoOfTimeVisitedInDay": "10"
    			},
    			{
    				"websiteName": "www.facebook.com",
    				"IpAddress": "26.58.193.10",
    				"timeSpent": "2 Hr",
    				"NoOfTimeVisitedInDay": "20"
    			}
    
    		]
    	}
    ]

    Now let us define POJO(Plain OLD JAVA OBJECT)

    As we can see our sample JSON has array of Students Object and student Object has some properties like id, firstName,lastName,email,gender,ipAddress and another List of Object called websitesVisited. websitesVisited has some more properties like websiteName,IpAddress,timeSpent,NoOfTimeVisitedInDay

    Now let us define POJO

    First define inner OBJECT called Website.

    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Websites {
    
      String websiteName;
      String ipAddress;
      String timeSpent;
      String NoOfTimeVisitedInDay;
    
      public Websites() {
    
      }
    
      public String getWebsiteName() {
        return websiteName;
      }
      public void setWebsiteName(String websiteName) {
        this.websiteName = websiteName;
      }
      public String getIpAddress() {
        return ipAddress;
      }
      public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
      }
      public String getTimeSpent() {
        return timeSpent;
      }
      public void setTimeSpent(String timeSpent) {
        this.timeSpent = timeSpent;
      }
      public String getNoOfTimeVisitedInDay() {
        return NoOfTimeVisitedInDay;
      }
      public void setNoOfTimeVisitedInDay(String noOfTimeVisitedInDay) {
        NoOfTimeVisitedInDay = noOfTimeVisitedInDay;
      }
    
      @Override
      public String toString() {
        return "Websites [websiteName=" + websiteName + ", ipAddress=" + ipAddress + ", timeSpent=" + timeSpent +
          ", NoOfTimeVisitedInDay=" + NoOfTimeVisitedInDay + "]";
      }
    
    
    
    }

    Now lets define the main object Students

    import java.util.List;
    
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement
    public class Students {
    
      String id;
      String firstName;
      String lastName;
      String email;
      String gender;
      String ipAddress;
      List < Websites > websitesVisited;
      public String getId() {
        return id;
      }
      public void setId(String id) {
        this.id = id;
      }
      public String getFirstName() {
        return firstName;
      }
      public void setFirstName(String firstName) {
        this.firstName = firstName;
      }
      public String getLastName() {
        return lastName;
      }
      public void setLastName(String lastName) {
        this.lastName = lastName;
      }
      public String getEmail() {
        return email;
      }
      public void setEmail(String email) {
        this.email = email;
      }
      public String getGender() {
        return gender;
      }
      public void setGender(String gender) {
        this.gender = gender;
      }
      public String getIpAddress() {
        return ipAddress;
      }
      public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
      }
      public List < Websites > getWebsitesVisited() {
        return websitesVisited;
      }
      public void setWebsitesVisited(List < Websites > websitesVisited) {
        this.websitesVisited = websitesVisited;
      }
      @Override
      public String toString() {
        return "Students [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email +
          ", gender=" + gender + ", ipAddress=" + ipAddress + ", websitesVisited=" + websitesVisited + "]";
      }
    
    
    
    }

    If you notice students Object has properties called List websitesVisited.

    Now write post method

    @POST
    @Consumes({
      MediaType.APPLICATION_JSON
    })
    @Produces({
      MediaType.APPLICATION_JSON
    })
    @Path("JsonPostExample")
    public String JsonPostExample(@PathParam("studentId") String studentId, List < Students > studentS) {
      System.out.println(studentS.toString());
      // Do whatever you want to do with the object
      return studentId;
    
    }

    I hope it helps.