Monday, August 4, 2014

How To Return JSON As Output From Play Controller

Many times the client expects a JSON response from the controller here is how you can do it.  As you may be aware all Play controllers return object which is of type play.mvc.Result.  So how can you convert the response to JSON, especially if you are returning any object?

Assume that there is a object of Employee class which needs to be returned as JSON. Here is how you can return the employee object as JSON.

import java.io.ByteArrayOutputStream;
import com.fasterxml.jackson.databind.ObjectMapper;

    public static Result employeeAsJson(Long id) throws Exception{
        Employee employee = Employee.findById(id); //Find the employee
        response().setContentType("application/json"); //Sets the content type

        ByteArrayOutputStream ba = new ByteArrayOutputStream();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(ba,employee); //convert the employee object to byte array output stream
        return ok(ba.toByteArray()); //return byte[ ]
    }

Since the content type of the response is set as Json you will get Json as output.

How to Convert List of Objects to Json in Play Framework?

We saw above how easily we can convert an object to Json output from a controller method.  How about a list of object?  That is also very simple.  In fact ObjectMapper class can easily do this for you as well.


import java.io.ByteArrayOutputStream;
import com.fasterxml.jackson.databind.ObjectMapper;

    public static Result employeesAsJson() throws Exception{
        List<Employee> employees = Employee.findAll(); //Return all employees as List
        response().setContentType("application/json"); //Sets the Content Type to json

        ByteArrayOutputStream ba = new ByteArrayOutputStream();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.writeValue(ba,employees); //Convert the employee object to byte array output stream
        return ok(ba.toByteArray()); //return byte[ ]
    }

As you can see the change was just sending the list of Employee to the writeValue method.  You will see output similar to below listing...

[{"id":1,"firstName":"Shailesh","middleName":"","lastName":"Sharma","emailId":"shaileshs@yash.com","contactNumber1":null,"contactNumber2":null,"customerUserId":null,"grade":"EE"},{"id":33,"firstName":"Abhijeet","middleName":"A","lastName":"Sawant","emailId":"abhijeet@yash.com","contactNumber1":null,"contactNumber2":null,"customerUserId":null,"grade":"E1"},{"id":34,"firstName":"Madhu","middleName":"","lastName":"Patel","emailId":"madhu@yash.com","contactNumber1":null,"contactNumber2":null,"customerUserId":null,"grade":"EE"},{"id":35,"firstName":"Chetan","middleName":"","lastName":"Bhagat","emailId":"chetan@yash.com","contactNumber1":null,"contactNumber2":null,"customerUserId":null,"grade":"E4"},{"id":41,"firstName":"Ashwin","middleName":"","lastName":"Patil","emailId":"aaa@aamm.com","contactNumber1":null,"contactNumber2":null,"customerUserId":null,"grade":"EE"},{"id":65,"firstName":"Another","middleName":"","lastName":"Name","emailId":"another@yash.com","contactNumber1":null,"contactNumber2":null,"customerUserId":null,"grade":"E5"}]

No comments:

Post a Comment