Example usage for com.liferay.portal.kernel.json JSONArray toString

List of usage examples for com.liferay.portal.kernel.json JSONArray toString

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.json JSONArray toString.

Prototype

public String toString(int indentFactor) throws JSONException;

Source Link

Usage

From source file:com.liferay.samplerestservice.rest.SampleRestService.java

License:Open Source License

@Consumes(MediaType.TEXT_PLAIN)
@GET/*  w  w  w  .  ja  v  a  2 s.  c om*/
@Path("blogs")
@Produces(MediaType.APPLICATION_JSON)
public Response getBlogsEntries(@QueryParam("start") Integer start, @QueryParam("end") Integer end) {

    ResponseBuilder responseBuilder = null;

    try {
        JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

        List<BlogsEntry> blogsEntries = BlogsEntryLocalServiceUtil.getBlogsEntries(start, end);

        for (BlogsEntry blogsEntry : blogsEntries) {
            jsonArray.put(getBlogsEntryJSONObject(blogsEntry));
        }

        responseBuilder = Response.ok(jsonArray.toString(4), MediaType.APPLICATION_JSON);

        return responseBuilder.build();
    } catch (Exception e) {
        responseBuilder = Response.status(Response.Status.INTERNAL_SERVER_ERROR);

        responseBuilder.entity(JSONFactoryUtil.serializeThrowable(e));

        return responseBuilder.build();
    }
}