Sling Post Servlet 正是您所寻找的。具体导入操作:
https://sling.apache.org/documentation/bundles/manipulating-content-the-slingpostservlet-servlets-post.html#importing-content-structures-1
但是,对于对象,您需要将 JSON 结构作为值对。这是一个基于您的示例 json 的示例:
{
"node": "parent",
"childs": {
"child1": {
"_id": "5fc9dadaca52c28bb40011ee",
"index": 0,
"tags": [
"laboris",
"minim"
]
},
"child2": {
"_id": "5fc9dada30930ef9d77c6d91",
"index": 1,
"tags": [
"duis",
"est"
]
}
}
}
从前端
这是一个使用 json 文件调用 sling post servlet 的示例。您可以在 JS 中遵循相同的模式,但指定 :content 而不是 :contentFile
curl http://localhost:4502/content/newTree \
-u admin:admin \
-F":operation=import" \
-F":contentType=json" \
-F":replace=true" \
-F":replaceProperties=true" \
-F":contentFile=@my-json-file.json"
从后端
您也可以从后端创建一个 POST 请求:
@Component(
service = Servlet.class,
property = {
ServletResolverConstants.SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
ServletResolverConstants.SLING_SERVLET_PATHS + "=" + "/bin/services/test"
})
public class Test extends SlingSafeMethodsServlet {
@Reference
private SlingRequestProcessor requestProcessor;
@Reference
private RequestResponseFactory requestResponseFactory;
@Override
protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws IOException {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
final Map<String, Object> parameters = ImmutableMap.of(
":operation", "import",
":contentType", "json",
":replaceProperties", "true",
":replace", "true",
":content", json
);
final HttpServletRequest postRequest = requestResponseFactory
.createRequest(HttpConstants.METHOD_POST, "/content/newTree", parameters);
final HttpServletResponse postResponse = requestResponseFactory.createResponse(output);
try {
requestProcessor.processRequest(postRequest, postResponse, request.getResourceResolver());
} catch (ServletException e) {
// handle exception
}
}
}