Complex types in a request of a published Rest-service

0
Does anyone has an example how to publish a RestService with a complex-type (like order and orderline) as an request? I want to create an mendix app that produces a rest-service, but I can’t figure out how to transform this json-request to mendix entities post-request {         "name": "Jones",         "phone": "01-23425346",         "order_line": [{             "product": "milk",             "quantity": 3         },         {             "product": "beer",             "quantity": 3         }] } store the data in:   The microflow: debug info: Solution I changed the association like: This works! Thanks for helping. I think it is a little bit strange. One order has n lines, you can't define this in your domainmodel. 'n orders has n lines' works and this is good enough.   
asked
3 answers
1

Hi Peter,

Could you also provide an updated screenshot of your domain model? 

What I think goes wrong is that when using the REST services module, the main entity (in your case 'Order') should be the owner of the association, and the association should be a reference set (many to many). Please see this and the other examples from https://github.com/mendix/RestServices/. Specifically check the way the OrderLines association has been set-up between OrderView and OrderLineView. 

Example Domain model

When using 'OrderView' as main object, serializing this would result in the following JSON. Deserialization works exactly the same, in order to deserialize the JSON below to your domain model, your domain model should be set up as shown above.

{
  "Nr": 1,
  "Total": 9.4,
  "OrderCustomer": "http://localhost:8080/rest/customers/3",
  "OrderLines": [
    {
      "Description": "Coffee Biscuits 36pcs",
      "Amount": 2,
      "Price": 0.89
    },
    {
      "Description": "Dark Coffee 36pads Limited Edition",
      "Amount": 3,
      "Price": 2.54
    }
  ]
}

Hope this helps!

answered
0

Hi Peter,

  This is indeed possible, and the output is controlled by your domain model.  You are very close in what you have. To have 'orderline' be an array included as part of the order in JSON, you will need to:

1) Have the association 'Entity_Order' re-named to 'orderline' as Mendix expects the associations, not the entity name' to drive the labeling of arrays

2) This association should be many-to-one as you have in your example

3) REST calls should be made with non-persistent entities

 

Hope this helps,

Rob

answered
0

Thanks Rob and Allard

 

I changed the association like:

This works! Thanks for helping. I think it is a little bit strange. One order has n lines, you can't define this in your domainmodel. 'n orders has n lines' works and this is good enough. 

Peter.

answered