Passing composite object parameter to jersey in Restful Java web

How to pass complex object parameter to jersey

In this tutorial, I will try to show how to pass composite object as a parameter to jersey’s endpoint java application.

It seems like you are working on RESTful API based java application. In that case, chances are high that you are working with jersey.

If you are not familiar with jersey, jersey is an implementation of JAX-RS APIs. Hence, it is a seamless framework by which API based java projects can be implemented.

As of this writing, the current version of jersey is 2.23.

To conclude, jersey would allow a programmer to handle basic http conversation b/n the client and server. Client would be sending data through GET, POST, PUT, DELETE with a payload that can be json, xml or any other known and accepted types.

Like wise, jersey also returns the response with json, xml or plain text formats.

Unfortunately, I might not go deeper than this for this post, but I will come back with jersey 2.0 tutorial soon.

What to accomplish

I will show how to pass compound object in the json format and how to accept that object using jersey.

Let’s say I have Department and Course objects


class Department {
    private Long id;
    private String name;
    private String faculty;
    
    ...getters and setters 
}

and


class Course {
    private Long id;
    private String title;
    private String credits;
    private Department department;
    
    ... getters and setters
}

On the above example, the course class has a department object and hence a composite class.

The tutorial shows how to pass such composite class to jersey’s end point.

What is the problem?

You might ask, what is the problem? I can pass this as normal JSON payload and get done with it?? Yes you can do that too.

But there is one leverage that can be taken from jersey. Jersey would match the json payload to your object directly.

For example you can pass something like {"name":"computer science", "faculty":"informatics"} and jersey would map it to the Department object.

The part that would be handling this from the jersey side would look like


@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Task addDepartment(Department department) {
    .... logic goes here..
}

On the above example, jersey would automatically map the payload given in json format to Department object assigning name=computer science and faculty=informatics

So, how to pass composite/complex objects and map it

So, in the case of composite object, objects that contain another object, how should it be passed to jersey.

In the example given, how to pass course object?

The solution is to pass the json object with the same structure as that of the composite object.

{"title":"Operating Systems", "credit":"3", "department":{"id":"1", "title":"computer science", "faculty":"informatics"}}

In this case, passing the above payload to jersey would make sure that the course object is perfectly mapped. If it not passed this way, the department member variable would be assigned null.

When to use it

Finally, this is handy to when you are working on objects for update or add and you need the objects being embedded to be populated as well. Most of the time, you might need only to pass the id of the object.

The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

String Ordered Permutation Algorithm Problem

setting JAVA_HOME on mac osx

hello world weblogic – hello world tutorial on weblogic

Consuming SOAP tutorial – Using java eclipse

connect to sftp from php

Leave a Reply

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

*
*