泽西,杰克逊和J&C的JSON格式化谷歌GSON使用Curl命令

时间:2012-12-14 01:44:51

标签: json curl jersey jackson gson

使用Java 6,Tomcat 7,Jersey 1.15,Jackson 2.0.6(来自FasterXml maven repo),&谷歌GSON 2.2.2, 我试图打印JSON字符串,因此它将通过curl -X GET命令行缩进。

我创建了一个简单的Web服务,它具有以下架构:

我的POJO(模型类):

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
}

使用Utility Class,我决定按如下方式对POJO进行硬编码:

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;
    }
}

我的网络服务:

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 com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
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());
            Gson gson = new GsonBuilder().setPrettyPrinting().create();
            JsonParser parser = new JsonParser();
            JsonElement jsonElement = parser.parse(uglyJsonString);
            System.out.println(gson.toJson(jsonElement));
            return gson.toJson(jsonElement);
    }
}

当我使用以下方式运行时:

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

我在Eclipse的控制台中得到了这个(这正是我想要的curl命令):

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

但是从上面列出的命令行curl命令,我得到了这个 (在\ n之后有4个空格但是JavaRanch的论坛没有显示它):

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

如何使用curl命令使JSON格式与我在Eclipse控制台中获得的格式相同?

感谢您花时间阅读此内容......

0 个答案:

没有答案