Java – jax-rs, mapping to JSON without overhead?

I am using jax-rs to create restful web services in Java I get a lot of overhead in the generated JSON

Data class:

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

Services:

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

Production:

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

I want it to produce:

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

What is the easiest way to achieve this goal? I'm free to redefine my data classes, but I can't know the key or size of the map in advance

Solution

The simplest way is to use list < pair > instead, pair is just a java bean with two properties

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>