javaweb-servicesjava-ws

Java WS RS - Post Entity Complex Object


Currently trying to call a service which needs:

{
  "a": [
    {
      "a1": "stuff",
      "a2": "stuff",
      "a3": "stuff"
    }
  ],
  "b": "stuff",
  "c": "stuff",
  "d": "stuff"
}

I need to post my json query via java ws rs and was wondering how I could wrap all of this in an entity.

I tried making 2 wrappers:

class A {
   String a1;
   String a2;
   String a3;
}

class Wrapper {
   A a;
   String b;
   String c;
   String d;
}

But upon posting the entity:

webResourceEndPoint.request(MediaType.APPLICATION_JSON)
            .post(Entity.json(wrapper);

I get an error 400.

Seems it can't read my json or understand the wrapper. How should I handle this query?


Solution

  • The a JSON property is an array (see the [ and the ]).

    Hence the a field of the Wrapper class must be defined as a List<A>:

    class Wrapper {
       List<A> a;
       String b;
       String c;
       String d;
    }