Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: MOTION-157
HTML Wrap
floatright
classmenu

Related topics:

This page provides an overview of the different ways of retrieving JSON for JCR-based content in Magnolia as well as listing their respective advantages and disadvantages. 

...

Expand
titleClick to see the resulting JSON
Code Pro
languagejs
linenumberstrue
{
  "results": [
    {
      "@name": "Kyoto",
      "@path": "/magnolia-travels/Kyoto",
      "@id": "b475f27e-2929-427b-9517-815118a3b36e",
      "@nodeType": "mgnl:content",
      "body": "<p>Erleben Sie die Sch&ouml;nheit, die Kyoto durchdringt und umgibt. Kyoto ist ber&uuml;hmt f&uuml;r viele Dinge, darunter unz&auml;hlige <a href=\"http://www.japan-guide.com/e/e2058.html\" target=\"_blank\">Tempel</a>, <a href=\"http://www.japan-guide.com/e/e2059.html\" target=\"_blank\">Schreine</a> und andere faszinierende historische Bauwerke.</p>\n<p>Kommen Sie mit uns auf einen Besuch der Stadt der zehntausend Schreine. Wir besuchen To-ji, Ginkaku-ji, Kōzan-ji und ihre bemerkenswerten G&auml;rten, bevor wir einen Abstecher zu den Bergen in der Umgebung der Stadt machen. Sie werden es genie&szlig;en, einen Tag im gr&uuml;nen Miyama und einen Tag mit Wandern in Shizuhara zu verbringen. Sie werden nie wieder wie bisher &uuml;ber Japan denken.</p> ",
      "name": "Kyoto",
      "description": "Die natürliche Seite Japans",
      "destination": [
        "7ec72c48-c33f-418e-b2ff-44cfb4bbb1f2"
      ],
      "location": "Kyoto, Japan",
      "tourTypes": [
        "d2245867-ecaa-4b4e-8743-e0c939be68b7",
        "415025c6-e4b5-4506-9384-34f428a52104"
      ],
      "author": "Magnolia Travels",
      "duration": "7",
      "image": "jcr:44689d29-5966-4d41-8fd4-2dc7da783528",
      "@nodes": []
    }
  ]
}

This result is returned for the endpoint called ep with the following configuration:

Code Block
languageyaml
class: info.magnolia.rest.delivery.jcr.v2.JcrDeliveryEndpointDefinition
workspace: tours
includeSystemProperties: false

Pros

  • Convenient and simple to use JSON structure.
  • Supports RESTful URLs.
  • Highly configurable via YAML file in a light module or in the JCR configuration workspace.
  • Supports multiple endpoint configurations to serve different needs.
  • Provides a fine-grained security.
  • Supports localization.
  • Can resolve referenced nodes from other workspaces,including image renditions, within one request.

...

However, having the "proper" content type may not be required. In many use cases it is sufficient to use the FreeMarker renderType that responds with text/html .

Renderer configuration

...

-proyaml

The above example requires the Magnolia specific !inherit directive available since Magnolia 5.5.6

Note that you can extend the default freemarker renderer.

Code Block
language
yml
title/module-a/renderers/json.yaml
urlhttps://git.magnolia-cms.com/projects/DOCUMENTATION/repos/having-fun-with-yaml/raw/light-modules/module-a/renderers/json.yaml?at=master
Localtab
titleJCR node
Advanced Tables - Table Plus
enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse

Node name

Value

Mgnl f
modules

Mgnl f
rendering

Mgnl f
renderers

Mgnl n
json

Mgnl p
contentType

application/json

Mgnl p
extends

../freemarker

Mgnl p
type

json

Template definition

Code Pro
title: jsonprovider0815
templateScript: /ftl-json-provider/templates/pages/jsonprovider0815.ftl
renderType: json
#renderType: freemarker
visible: true

Turning a template-based JSON provider into a RESTful API with virtualURIMapping

You can create a template that accepts request parameters to specify the JSON content to be returned. In the example below, you can specify the workspace and the path of the node.

!inherit:freemarker
contentType: application/json

Template definition

Code Pro
title: jsonprovider0815
templateScript: /ftl-json-provider/templates/pages/jsonprovider0815.ftl
renderType: json
#renderType: freemarker
visible: true

Turning a template-based JSON provider into a RESTful API with virtualURIMapping

You can create a template that accepts request parameters to specify the JSON content to be returned. In the example below, you can specify the workspace and the path of the node.

Code Pro
[#assign path = ctx.path!"undefined" /]
[#assign workspace = ctx.workspace!"tours" /]

[#assign response = '{ "error" : "Missing path parameter." }' /]

[#if path!="undefined"]
  [#assign contentNode = cmsfn.contentByPath(path, workspace)]
  [#assign response = jsonfn.from(contentNode).add("@path", "name", "description").wrapForI18n().print() /]
[/#if]

${response
Code Pro
[#assign path = ctx.path!"undefined" /]
[#assign workspace = ctx.workspace!"tours" /]

[#assign response = '{ "error" : "Missing path parameter." }' /]

[#if path!="undefined"]
  [#assign contentNode = cmsfn.contentByPath(path, workspace)]
  [#assign response = jsonfn.from(contentNode).add("@path", "name", "description").wrapForI18n().print() /]
[/#if]

${response}

Create the page with the JSON-providing template at /getjson. You can call it with the following URL: 

Mgnl mini code snippet

http://localhost:8080/magnoliaAuthor/getjson? workspace=tours & path=/magnolia-travels/Kyoto

If you want to use a RESTful URL instead:

Mgnl mini code snippet

http://localhost:8080/magnoliaAuthor/getjson/tours /magnolia-travels/Kyoto

Add the following virtual URI mapping:

...

enableHeadingAttributesfalse
enableSortingfalse
classm5-configuration-tree
enableHighlightingfalse

...

Node name

...

Value

...

Mgnl f
modules

...

Mgnl f
<your-module>

...

Mgnl f
virtualURIMapping

...

Mgnl n
getjson-fromWorkspace-byPath

...

Mgnl p
class

...

Mgnl p
fromURI

...

Mgnl p
toURI

...

getjson. You can call it with the following URL: 

Mgnl mini code snippet

http://localhost:8080/magnoliaAuthor/getjson? workspace=tours & path=/magnolia-travels/Kyoto

If you want to use a RESTful URL instead:

Mgnl mini code snippet

http://localhost:8080/magnoliaAuthor/getjson/tours /magnolia-travels/Kyoto

Add the following virtual URI mapping:

Image Added

Pros

  • Highly configurable.
    • Fully customizable when assembling JSON manually.
    • Decent customizable when using jsonfn.
  • No Java required.
  • Localization support.
  • Delivery of personalized variants of pages or components is possible (see personalization).

...

Multiexcerpt
MultiExcerptNamesame-origin-policy

According to the same-origin policy:

(...) a web browser permits scripts contained in a first web page to access data in a second web page, but only if both web pages have the same origin.

Wikipedia

Therefore, if your application resides within a domain that is different from the domain that hosts your data, the web browser will actively prevent accessing content stored under the host domain.

An elegant solution to solve the same-origin policy without changing Apache configuration is the Magnolia Add HTTP Headers filter. This fiter filter is available out-of-the-box; you only need to configure it.

...