1

Exactly the same question has been asked here: JAX-RS - JSON without root node But the solution doesn't work for me because my server doesn't have any other interfaces besides REST, thus file WEB-INF/web.xml doesn't exists. Below is the original question:

I have a restfull webservice, and the response is:

{
    "cities": [
        {
            "id": "1",
            "name": "City 01",
            "state": "A1"
        },
        {
            "id": "2",
            "name": "City 02",
            "state": "A1"
        }
    ]
}

But I want this

    [
        {
            "id": "1",
            "name": "City 01",
            "state": "A1"
        },
        {
            "id": "2",
            "name": "City 02",
            "state": "A1"
        }
    ]

How I can configure JAX-RS to produces JSON without root node using only JAX-RS feature, and not implementation specific feature? My code needs to be portable accross any appserver.

Thanks

Community
  • 1
  • 1
Sergey
  • 324
  • 4
  • 9
  • See also [this answer](http://stackoverflow.com/questions/11027713/unwrap-a-element-in-jackson-jaxb) – Thomas Apr 21 '13 at 04:14
  • 1) JAX-RS is a REST framework, it doesn't know or care about JSON 2) Your second example is invalid JSON and will be rejected by pretty much every JSON library out there. – Perception Apr 21 '13 at 06:04
  • @Thomas Thanks for the link. I have already tried it and it has no effect. But if I call rootUnwrapping(false) then I get 2 wrappers. See this for details: http://stackoverflow.com/questions/2199453/how-can-i-customize-serialization-of-a-list-of-jaxb-objects-to-json – Sergey Apr 21 '13 at 18:48
  • The accepted answer at http://stackoverflow.com/questions/2199453/how-can-i-customize-serialization-of-a-list-of-jaxb-objects-to-json also refers to web.xml which I don't have as mentioned above. – Sergey Apr 21 '13 at 18:57
  • @Perception Yes, you are right, I have copied it from the other post and it had an extra pair braces. I have fixed the question now. – Sergey Apr 21 '13 at 19:02

1 Answers1

1

I have finally solved the problem. All I had to do is to mark the checkbox "Package" in Project Properties/Compile/GlassFish Server 3+ Jersay

Sergey
  • 324
  • 4
  • 9