JAX-RS,Map< String,String>到JSON没有开销? [英] JAX-RS, Map<String,String> to JSON without the overhead?

查看:115
本文介绍了JAX-RS,Map< String,String>到JSON没有开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAX-RS在Java中创建安静的Web服务。我在生成的JSON中花费了很多开销。

I'm using JAX-RS to create restful webservices in Java. I am getting to much overhead in the produced JSON.

数据类:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Test {

    private Map<String,String> data;

    Test() {}

    public Test(Map<String,String> data) {
        this.data = data;
    }

    public Map<String, String> getData() {
        return data;
    }
}

服务:

@GET
@Path("/test")
@Produces("application/json; charset=UTF-8;")
public Test test() {
   Map<String,String> map = new HashMap<String,String>();
   map.put("foo", "bar");
   map.put("bingo", "bongo");
   return new Test(map);
}

产生:

{"data":{"entry":[{"key":"foo","value":"bar"},{"key":"bingo","value":"bongo"}]}}

我想要产生:

{"data":{"foo":"bar","bingo":"bongo"}}

这是最简单的方法?我可以自由地重新编译我的数据类,但是我不能提前知道地图的键或大小。

What is the simplest way to achive this? I am free to redifine my data class but I can't know in advance the keys or size of the map.

推荐答案

方式将使用列表< Pair> 而不是 Pair 只是一个具有两个属性的Javabean。

Simplest way would be using List<Pair> instead where Pair is just a Javabean with two properties.

这篇关于JAX-RS,Map&lt; String,String&gt;到JSON没有开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆