Take the 2-minute tour ×

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

Data class:

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;


@Produces("application/json; charset=UTF-8;")
public Test test() {
   map.put("foo", "bar");
   map.put("bingo", "bongo");
   return new Test(map);



I would like it to produce:


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.

share|improve this question
Given the overhead of the web request, are you sure the json format is worth optimizing? (In other words, did profiling your application lead you to believe the format of the json data was a performance issue?) Remember... Premature optimization is the root of all evil in programming –  Rob Andrews Sep 4 '12 at 22:18
add comment

3 Answers

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

share|improve this answer
add comment

"JAXB spec defines a special handling for Map when it's used as a property of a bean."

In light of that, I don't think there is much you can do (currently) except, perhaps, to write your own MessageBodyWriter (https://jsr311.dev.java.net/nonav/javadoc/javax/ws/rs/ext/MessageBodyWriter.html)

share|improve this answer
add comment

If you don't use JAXB annotated objects, but simple POJO's, you can get the correct behaviour by simply enabling the POJO mapping feature, together with the Jackson JSON library.

So in web.xml, if you're using the filter (similar for servlet), your configuration should be:


And just depend on the jersey-json dependency in your maven config, or download it from the website and put jackson in your classpath. See also this answer and this answer, and this blog post. And why on earth this is not standard behaviour I do not know.

share|improve this answer
add comment

Your Answer

