Max
Max

Reputation: 643

Play Framework: How to bind a complex REST request to a Controller method

Working on a REST API with Play Framework.

I have a requirement to support a RESTful request containing the "order" with multiple "line items".

In terms of the "POST data", I see it like: (split into multi-lines for clarity)

OrderId=123&OrderType=regular&
ItemNum=1&ItemID=78&quantity=2&discount=20&
ItemNum=2&ItemID=70&quantity=1&
ItemNum=3&ItemID=75&quantity=1&discount=10

Note that I have an issue to require all the "line items" to come with a full set of data. In the example above, the 2nd item has no discount. Since I cannot "force" developers using the API to work with my own "wrapper", I want to leave some flexibility.

I would like to map it to something like:

method(int orderID, string orderType, Item[] items)

However, I failed to find something appropriate in the docs.

What's the right way? Should I build my own parser of the HTTP request data? Any alternative way to format the POST data - as long as it is ok with REST guidelines - is also acceptable.

Thanks Max

Upvotes: 2

Views: 601

Answers (1)

Codemwnci
Codemwnci

Reputation: 54924

To map an array of Pojo objects, then you need to put item. in front of the item object. Just like you map an object in a form. Then, you should specify that it is an array using the standard array syntax.

I would do something like the following

orderId=123&orderType=regular&
item[0].ItemNum=1&item[0].ItemID=78&item[0].quantity=2&item[0].discount=20&
item[1].ItemNum=2&item[1].ItemID=70&item[1].quantity=1&
item[2].ItemNum=3&item[2].ItemID=75&item[2].quantity=1&item[2].discount=10

Upvotes: 2

Related Questions