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 )

1
  • Have you tried? What happened? If something went wrong, please show us the code and describe what went wrong and where. Commented Jun 14, 2017 at 8:20

3 Answers 3

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": [...]
        }
      }
    ]
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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<string,object> 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 )
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"
   }
}

1 Comment

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 ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.