Published REST Example Project

0
Looking for a sample project, in 7.11 that demonstrates a REST API being published, does anyone have anything? Looking through the release notes it mentions a Message Definitions window but I cannot find this anywhere. Created a Published Rest Service, Resources & Operations. Created a microflow but the post shows an Export to JSON and Create HTTP Header & Response but no instruction on detail behind these. https://www.mendix.com/blog/mendix-7-11-offers-enhancements-rest-publish-api-creation-added-capabilities-web-modeler/ 
asked
4 answers
12

Cliff,

For the export to JSON you'll need an export mapping.

In the example I'll use the follwoing domain model:

I'll create a publish rest service, with a resource called Order and a Get operation that uses a microflow GetOrder:

The microflow looks like this to start with, I added 2 actions to retrieve the order based on the ID being provided:

Add a mapping by adding an activity to your microflow with the type 'Export with mapping'.

In the activity select a mapping via the select button, and as you will not have this select the new button in the opened dialog to create a new mapping.

Navigate to the new mapping and open the mapping. In the tab that is then opened select the option 'Select Elements' in the top bar.

A dialog opens which allows to set the mapping to an XML scheme, JSON structure, web service operation or Message definition. Depending on what you need the API to return make a choice.

Let say you choose a message definition. As you don't have one yet use the new button in the dialog to create one.

In the dialog that now opens select the entity that you want to return and in the structure part select the attributes and optionally associated objects and attributes.

Then in the underlying dialog select the parts of the structure you want to return in the mapping (probably the same attributes and objects selected in the message definition).

Now the mapping definition tab will show you the objects in the mapping. Map the objects and attributes to your domain model objects. 

Drag the objects from the connector panel or double click the grey objects to select the domain model objects. Map the attributes in the dialog that is opened after mapping an domain model object:

Make sure that in the microflow using the export mapping to select the xml or json as option and store the result of the mapping as string.

Also make sure the parameter field is mapped.

In the create httpresponse action that follows, map the content to the string as below:

At the end action of the microflow set the return value to the newly created httpresponse object and your microflow looks like:

I hope this provides you with enough information on publishing the rest service, or at least a good starting point.

answered
0

Erwin's how-to now published in our documentation: How to Publish a REST Service

answered
3

Hi Cliff! Erwin's explanation is very good, so I'm certain you should be able to create your own published service with his information.

 

I'd still like to take this opportunity to let everybody with a similar question know that I'm in the middle of creating a YouTube tutorial on REST publish with some more in-depth information on what options and choices you have. I'll be sure to include Erwin's tips and if anyone else has tips/tricks they'd like to share please let me know!

answered
2

Hi Erwin,

great answer! Steps are clear and easy to follow. I would like to add one thing: it is a good idea to add a HTTP header to the response, indicating the content type of the response. You could do that by creating a HttpHeader object, after creating the HttpResponse. Set the member Key to 'Content-Type' and the Value to 'application/json' (or 'application/xml' if you're response contains XML rather than JSON). Set the System.HttpHeaders association to your HTTP response.

By setting the content type header correctly, the response will be parsed automatically by most third party tools.

answered