1

i would like to know if rest api while consuming input parameter can do the following:

let's say my json object have the following parameters:

string name;

string adress;

hashmap<string,object> content;

and here's an exemple of what can be sent:

{
    "name": "AZ",
    "adress": "US",
    "content": {
        "clients": [
            {
                "client_ref":"213",
                "commands" : {
                    "subCommands": [
                        {
                            "num":"1",
                            "price":"10euro"
                        },
                        {
                            "num":"12,
                            "price":"10euro"
                        }
                    ]
                }
            },
            {
                "client_ref":"213",
                "commands" : {
                    "subCommands": [
                        {
                            "num":"1",
                            "price":"10euro"
                        },
                        {
                            "num":"12,
                            "price":"10euro"
                        }
                    ]
                }
            }
        ]
    }
}

the question is can rest build the hashmap where the object can itself have n child of hashmap type ... ?

(i'm using jersey as rest implementation )

Cœur
  • 37,241
  • 25
  • 195
  • 267
Anis ZARRAA
  • 21
  • 1
  • 1
  • 3
  • Have you tried? What happened? If something went wrong, please show us the code and describe what went wrong and where. – Erwin Bolwidt Jun 14 '17 at 08:20

3 Answers3

1

Assuming that you have a JSON provider such as Jackson registered and your model class looks like:

public class Foo {

    private String name;
    private String address;
    private Map<String, Object> content;

    // Getters and setters
}

The following resource method:

@Path("foo")
public class Test {

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Response post(Foo foo) {
        ...
    }
}

Can handle a request like:

POST /api/foo HTTP/1.1
Host: example.org
Content-Type: application/json

{
  "name": "AZ",
  "adress": "US",
  "content": {
    "clients": [
      {
        "client_ref": "213",
        "commands": {
          "subCommands": [...]
        }
      },
      {
        "client_ref": "213",
        "commands": {
          "subCommands": [...]
        }
      }
    ]
  }
}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
0

content is an Object, not a map.

"content": {
    "clients": [
        {
            "client_ref":"213",
            "commands" : {
                "subCommands": [
                    {
                        "num":"1",
                        "price":"10euro"
                    },
                    {
                        "num":"12,
                        "price":"10euro"
                    }
                ]
            }
        },
        {
            "client_ref":"213",
            "commands" : {
                "subCommands": [
                    {
                        "num":"1",
                        "price":"10euro"
                    },
                    {
                        "num":"12,
                        "price":"10euro"
                    }
                ]
            }
        }
    ]
}

And this is Java Object presentation.

public class Content {

    private List<Client> clients;

    //Getters and setters
}


public class Client {

    private String clientRef;
    private List<Command> commands;

    //Getters and setters
}
//And so on, define other classes.

To answer your question, yes, you can build a map. Check this example, please. It tells how to parse an unknown json (in case you don't know the exact structure of your json object). https://stackoverflow.com/a/44331104/4587961

Then you can build a map with fields Map<String, Object> where some values of this map will be nested maps.

Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
  • thanks Yan, my example was missleading both of you, I have a rest API that will accept a Client JSON object that have a hashmap content property, my question was is the REST api able to convert automaticlly any complex structure that has been sent to my service to its corresponding hashmap strcuture ? ( which mean we can deal with nested hashmap ) – Anis ZARRAA Jun 14 '17 at 09:22
0

you can use javax.ws.rs.core.GenericEntity to wrap collections with generic types (your HashMap).

@GET
@Path("/mapping")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getAllMapContents() {

    Map<String,Object> map = new HashMap<String,Object>();

    map.put("Hello", "World");
    map.put("employee", new Employee(1,"nomad"));

    GenericEntity<Map<String,Object>> entity = new GenericEntity<Map<String,Object>>(map) {};
    return Response.ok(entity).build();
}

I checked it and found it working Please find the response below. Thank you.

{
   "Hello": "World",
   "employee": {
      "id": 1,
      "name": "nomad"
   }
}
Graham
  • 7,431
  • 18
  • 59
  • 84
Praveen
  • 432
  • 1
  • 6
  • 14
  • my example was the other way arround, a rest ( POST, not GET ) that accept a client json object, that have a content hashmap of objects, knowing that i don't know the structure of the content that is going to be sent, we can end up with hashmap of objects that got N level of nested hashmap ... is the REST api able to handle this ? convert the received content JSON complex structure to an object that can have N level of nested hashmap .. – Anis ZARRAA Jun 14 '17 at 09:28