Learn by product

Select the product you would like help with:

Martini Logo
Select the product you would like help with:
Community Q&A

Ask the Community

How to create a dynamic response payload? e.g the body changes depending if it's a success or error

Hi Adam, You can make your Gloop services return dynamic output that can either describe a success or an error response or anything you want your service to return. You'll be able to achieve it with just a very simple configuration

To get the general idea, let's create a simple Gloop Service:

  1. Create a blank Gloop Service
  2. In your Gloop service, head over to the Input/Output view. On the right side is the output column. Use the content assist to quickly create a Gloop Model. You can also use the shortcut ⌥+M for Mac, alt+M for Windows and Linux. On the left side is the input column. Use the content assist and search for streams.model.Person to quickly create a new Gloop Model using a pre-made model that comes out-of-box with Martini Runtime. At this point your inputs and outputs should like this

  3. Go back on the output column in the Input/Output view and select the Gloop Model you have created. You should be able to see the available properties of your Gloop Model output in the Properties View. Look for the property Allow Extra Properties and set its value to true. This will make your empty Gloop Model accept any model and have the same properties with the model that has been mapped into it.
  4. Going back with your Gloop Service, in the Step Editor, use the content assist to quickly create a Map step (select the first entry to appear), Once created, highlight the step to make the Mapper View active. From here you can start mapping the input model to the output model by dragging and dropping your input model over to the output model.

  5. At this point, you would want to see how does this make the output of this service return a dynamic output so we're going to add something that prints in the console. Again use the content assist to quickly create a step that does the logging by using the keyword info.
  6. Select the first entry that will appear in the selection. Once selected, a Set Expression Editor will appear. Use this Groovy code snippet to display the output model in the console "The output is:\n${model.asJson()}" (quotes included). And make sure that the Evaluate(⌘E) option is checked.

    The GString ${model.asJson()} will print the Gloop model output in JSON format

  7. You are now ready to run the service. If you have followed everything correctly you should be able to see a similar output below in the console


INFO  [Martini] The output is:
    "model": {
        "name": "John Doe",
        "age": 28

Notice that there is no declared property in the Gloop Model output, and yet Gloop was able to give you an output with properties similar to the input properties that was mapped in it. At runtime, if the service is invoked over HTTP, it will still figure out how to write it to XML, JSON, or YAML