简体   繁体   English

REST-如何减少Json响应

[英]REST - How to minify Json Response

I have a JAVA REST service that returns a list of objects. 我有一个JAVA REST服务,它返回对象列表。 Each object contains name, description, code. 每个对象都包含名称,描述,代码。 I want to minify response json 我想缩小响应json

{
    "objects": {
        "count": 10000,
        "list": [
            {
                "name": "1",
                "description": "foo",
                "code": "foo",
            },
            {
                "name": "2",
                "description": "bar",
                "code": "bar",
            },
            ...... (1.000 items)
        ]
    }
}

TO: 至:

{
    "a": {
        "b": 1000,
        "c": "a:objects,b:count,c:mapping,d:list,e:name,f:description,g:code",
        "d": [
            {
                "e": "1",
                "f": "foo",
                "g": "foo",
            },
            {
                "e": "2",
                "f": "bar",
                "g": "bar",
            },
            ...... (1.000 items)
        ]
    }
}

how can i do it, thanks. 我该怎么办,谢谢。

Even though we don't know what specific technologies you are using. 即使我们不知道您在使用什么特定技术。 I am going to make an assumption that you are using some sort of REST library like Spring or JaxRS and you are serializing POJOs into the JSON. 我将假设您正在使用某种REST库(例如Spring或JaxRS),并且正在将POJO序列化为JSON。 I will also make the assumption that you have everything setup and working for that configuration and I will focus on the output specifically using that setup. 我还将假设您已完成所有设置并为该配置工作,并且我将专注于使用该设置的输出。

If you are using something like Jackson for your POJO, you can add the following Annotation to your class: 如果您在POJO中使用Jackson之类的东西,则可以在类中添加以下注释:

public class MyResponseObject {
    @JsonProperty("a")
    private MyObject objects;

    public MyObject getObjects() { return objects; }
    public void setObjects(MyObject object) { this.objects = object; }
}

public class MyObject {
    @JsonProperty("b")
    private long count;
    @JsonProperty("d")
    private List<Item> list;

    // getters/setters
}

public class Item {
    @JsonProperty("e")
    private Sting name;
    @JsonProperty("f")
    private String description;
    @JsonProperty("g")
    private String code;

    // getters/setters
}

In regards to the mapping of what each of those mean, you can hard/code that mapping, but I don't think there is an automatic way to do that. 关于这些含义的映射,您可以对映射进行硬编码,但我认为没有自动的方式可以做到这一点。 You can also include what that mapping is in the JavaDoc for your method. 您还可以在该方法的JavaDoc中包括该映射的内容。 Another alternative is if this is an API that is public to other services, you can not only provide documentation, but also a packaged Jar with the POJOs that your API puts out. 另一个选择是,如果这是对其他服务公共的API,则不仅可以提供文档,还可以提供带有API推出的POJO的打包Jar。 This way all they have to do is include your jar file as a dependency and include them in the mapped classes. 这样,他们所需要做的就是将jar文件作为依赖项包含在映射类中。

I hope this helps guide you in the right direction. 我希望这有助于您朝正确的方向发展。

Also, if you don't use Jackson, but prefer JAXB use @XmlElement(name="a") 另外,如果您不使用Jackson,而是喜欢使用JAXB,请使用@XmlElement(name="a")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM