AEM JCR - 获得响应 JSON

AEM JCR - Get response as JSON

我们正在 JCR 中的特定位置下添加一些元数据:

POST /some/jcr/location/_jcr_content/json HTTP/1.1
Accept: application/json
Authorization: Basic YWRtaW46YWRtaW4=
Cache-Control: no-cache
Host: localhost:4502
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------554953211468377919703514
Cookie: cq-authoring-mode=TOUCH
Content-Length: 383
----------------------------554953211468377919703514
Content-Disposition: form-data; name="./value"

{ "test": "test" }
----------------------------554953211468377919703514
Content-Disposition: form-data; name=":cq_csrf_token"

ey***our csrf token***-c5Oa0
----------------------------554953211468377919703514--

但是当我们获取相同的资源时,响应类型是 test/html`:

GET /some/jcr/location/jcr:content/json/value HTTP/1.1
Accept: application/json
Cache-Control: no-cache
Host: localhost:4502
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Cookie: cq-authoring-mode=TOUCH

HTTP/1.1 200 OK
Date: Fri, 26 Feb 2021 13:49:38 GMT
X-Content-Type-Options: nosniff
Content-Type: text/plain;charset=utf-8 
Content-Length: 18
{ "test": "test" }

我们需要在 JCR 中添加什么配置,或者我们需要在我们的请求中编辑什么以确保 JCR returns 内容类型 application/json.

更新:正如 Sharath Madappa 回复的那样,您可以通过在位置后缀 .json 扩展名来请求 JSON 格式的数据。但是,这会导致以下格式:

{
    "value": "{ \"test\": \"test\" }"
}

虽然我希望它是:

{
    "test": "test"
}

在发出请求时使用 GET /some/jcr/location/jcr:content/json/value.json。 Sling 能够根据请求中的扩展 render/return 多种格式的资源。默认情况下,如果未提供扩展名,则使用 HTML。路径告诉哪个资源,扩展和选择器告诉如何以及什么呈现资源。

发布 JSON 内容然后将其检索为 Content-Type: application/json 可以通过文件上传而不是添加属性来完成。

这是一个 javascript 示例:

const data = { test: "test" };
const jsonData = new Blob([JSON.stringify(data)], { type: 'application/json' });
const formData = new FormData();
formData.append(`myFile.json`, jsonData, `myFile.json`);

return fetch(path, {
  method: 'POST',
  body: formData
});

此示例未考虑 Authn/Authz。

要以 application/json 格式获取数据,您只需向 JSON 文件的位置发出 GET 调用:

http://localhost:4502/path/to/the/folder/jcr:content/files/myFile.json

除了具有 JSON 格式响应之外,您还可以指定要达到的深度。

例如 JSON 格式的 1(仅节点)的深度:

curl -u <user>:<pass> -v <URL>/some/jcr/location/jcr:content/json/value.1.json

某路径下的所有子节点JSON格式:

curl -u <user>:<pass> -v <URL>/some/jcr/location.-1.json