eightechess
eightechess

Reputation: 56

How to receive dynamic values in restful webservice

@POST
@Consumes({"application/x-www-form-urlencoded","application/json","application/xml"})
@Produces(MediaType.TEXT_HTML)
public String returnItemLookup(
        @HeaderParam("authSessionID")String header,
        @PathParam("item_{the number of the item here}_name")String item_name,
        @PathParam("item_{the number of the item here}_quantity")int item_quantity)

Example:

@PathParam("item_1_name")String item_name,
@PathParam("item_1_quantity")int item_quantity,
@PathParam("item_2_name")String item_name,
@PathParam("item_2_quantity")int item_quantity,
@PathParam("item_3_name")String item_name,
@PathParam("item_3_quantity")int item_quantity

The web-service is supposed to collect and put the items in an array.

So my problem is, which param do I use that may allow placeholders for parameters to change dynamically?

Upvotes: 0

Views: 357

Answers (1)

nnunes10
nnunes10

Reputation: 550

I think that is not possible with JAX-RS. You should encapstulate the name and quantity in a Parameter Object. Then your method should accept a list of parameters:

@POST
@Consumes(MediaType.APPLICATION_JSON)
public String returnItemLookup(List<Parameter> parameters)
{
 ... 
}

Your JSON should look like this:

{
  "parameters": [
    {
      "item_name": value1,
      "item_quantity": value2
    },
    {
      "item_name": value3,
      "item_quantity": value4
    },
    ...
  ]
}

Upvotes: 1

Related Questions