1

Using Java 6, Tomcat 7, Jersey 1.15, Jackson 1.9.9, 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;
    }
}

MyWebService:

@Path("")
public class MyWebService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Family getFamily {
        return FamilyUtil.getFamily();
    }
}

Produces:

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

What I need to do is have it produce it in a more legible manner:

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

Just am seeking a way to implement so it can display some type of formatting with indentation / tabs.

Would be very grateful if someone could assist me.

Thank you for taking the time to read this.

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • 3
    Generally speaking, it should be up to the web service client (the entity making the request), to pretty print the JSON. Your service shouldn't return pretty-printed JSON as essentially you're delivering more bytes than necessary to convey the message. – Catchwa Dec 13 '12 at 00:37
  • Agree with Catchwa - use tools to pretty print the json on the machine making the request. Keeping extra spaces and carriage returns is actually a good thing - you're sending less data and your keeping the request smaller. – mawaldne Dec 13 '12 at 03:32

2 Answers2

2

Instead of returning a Family, you can just return a String (the web service caller won't notice a difference)

@Path("")
public class MyWebService {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getFamily {
        // ObjectMapper instantiation and configuration could be static...
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); 
        return mapper.writeValueAsString(FamilyUtil.getFamily());
    }
}

Although, as I said in my comment to your question, your service really shouldn't be doing this. It should be up to the web service caller to format the response into whatever format they need.

Catchwa
  • 5,845
  • 4
  • 31
  • 57
-1

I was able to get past this (the unnecessary quotes & newline escape sequences) by using the following code:

@GET
@Produces(MediaType.APPLICATION_JSON)
public Family getFamily() throws JsonGenerationException, 
                                 JsonMappingException, 
                                 IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); 
        Family family = FamilyUtil.getFamily();
        return mapper.readValue(mapper.writeValueAsString(family),
                               FamilyUtil.getFamily().getClass());
}

Using curl like this:

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

Yields:

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

Doesn't seem like anything happened...

Here are the dependencies listed in my pom.xml file:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.0.6</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.0.6</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.0.6</version>
</dependency>

Does anyone know what I am possibly doing wrong?

Are there other libraries that make this a whole let easier? Doesn't seem like Jackson is working...

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • 1
    Your code serialises a Family to JSON, reads in the JSON and constructs a Family instance, then returns that Family instance via Jersey (which then uses Jackson to serialise it to JSON but without any special configuration as I don't think you can configure the implicit JSON serialiser within Jersey) – Catchwa Dec 13 '12 at 23:50
  • You should probably either update your original question with this information, or post a new question altogether. This isn't an answer to your original question, yet you posted it as one. – Zero3 Aug 09 '15 at 11:31