REST: is it possible to skip some attributes from entity in request

2
Is it possible to skip some atrributes in a POST request object? So if I have for example the entity Customer with attributes Name and LastName they json generated will look like: {"Name":"Erik", "LastName":"Nobel" } The webservice I have to use does not allow me to send empty attributes. So if in this example the Name attribute would be empty or empty string the json should look like: {"LastName":"Nobel" }
asked
3 answers
0

When they do not allow you to send null then you have to adjust your business logic and prevent null/empty from being allowed. But what if you want to send null/empty as a real value, because null telling "nothing there, empty string" is a valid case, then you can't? I foresee a good discussion with the webservice provider how to get this solved. Interesting case.

answered
0

I recognise this problem very well: the webservice provider does not accept other attributes than specified (wouldn't argue about that). Currently, there is not really a solution in Mendix, but I've seen some preliminary work on JSON-mappings like XML-mappings which makes it possible to select the attributes you want to export to JSON. The expectation is that this will be made publicly available in Q3 of this year.

The current workaround is to have another (non-persistant) entity which corresponds to the definition and attributes needed for that specific request and copy the data to a new object of that entity and then make the webservice call using that object. In your example case: add an entity CustomerRequest having only the attribute LastName.

answered
0

You could also consider modifying the REST module's Java code to not include empty attributes when serializing JSON. You would want to modify JSONSerializer.java around line 153, to check and see if "value" is null, and skip the serialization process if so.

Note this affects all REST calls in your app (and is non-standard) unless you maintain 2 separate REST modules.

answered