JSON Formatting with Jersey, Jackson, & json.org/java Parser using Curl Command

Posted by socal_javaguy on Stack Overflow See other posts from Stack Overflow or by socal_javaguy
Published on 2012-12-14T01:40:57Z Indexed on 2012/12/14 5:03 UTC
Read the original article Hit count: 463

Filed under:
|
|
|
|

Using Java 6, Tomcat 7, Jersey 1.15, Jackson 2.0.6 (from FasterXml maven repo), & www.json.org parser, I am trying to pretty print the JSON String so it will look indented by the curl -X GET command line.

I created a simple web service which has the following architecture:

My POJOs (model classes):

Family.java

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Family {
    private String father;
    private String mother;

    private List<Children> children;

    // Getter & Setters
}

Children.java

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Children {
    private String name;
    private String age;
    private String gender;

    // Getters & Setters
}

Using a Utility Class, I decided to hard code the POJOs as follows:

public class FamilyUtil {

    public static Family getFamily() {
        Family family = new Family();
        family.setFather("Joe");
        family.setMother("Jennifer");

        Children child = new Children();
        child.setName("Jimmy");
        child.setAge("12");
        child.setGender("male");
        List<Children> children = new ArrayList<Children>();

        children.add(child);

        family.setChildren(children);
        return family;
    }
}

My web service:

import java.io.IOException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jettison.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import com.myapp.controller.myappController;
import com.myapp.resource.output.HostingSegmentOutput;
import com.myapp.util.FamilyUtil;

@Path("")
public class MyWebService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public static String getFamily() throws IOException, 
                                            JsonGenerationException, 
                                            JsonMappingException, 
                                            JSONException, 
                                            org.json.JSONException {
        ObjectMapper mapper = new ObjectMapper(); 
        String uglyJsonString = mapper.writeValueAsString(FamilyUtil.getFamily());
        System.out.println(uglyJsonString);
        JSONTokener tokener = new JSONTokener(uglyJsonString);
        JSONObject finalResult = new JSONObject(tokener);
        return finalResult.toString(4);         
    }
}

When I run this using:

curl -X GET http://localhost:8080/mywebservice

I get this in my Eclipse's console:

{"father":"Joe","mother":"Jennifer","children":[{"name":"Jimmy","age":"12","gender":"male"}]}

But from the curl command on the command line (this response is more important):

"{\n    \"mother\": \"Jennifer\",\n    \"children\": [{\n        \"age\": \"12\",\n        \"name\": \"Jimmy\",\n        \"gender\": \"male\"\n    }],\n    \"father\": \"Joe\"\n}"

This is adding newline escape sequences and placing double quotes (but not indenting like it should it does have 4 spaces after the new line but its all in one line).

Would appreciate it if someone could point me in the right direction.

© Stack Overflow or respective owner

Related posts about java

Related posts about JSON