如何在Sightly中显示JSON响应映射Pojo
我有一个吊索servlet调用第三方API并提供JSON回应。我使用杰克逊将JSON响应绘制了对Pojo课程的响应。现在,我必须在Sightly中显示这种动态获取和映射的响应。我该怎么做?响应映射后我被困
I have a sling servlet that invokes a 3rd party api and fetches a json response. I have mapped the json response to a pojo class using Jackson. I now have to display this dynamically fetched and mapped response in sightly. How do i do that? I am stuck after the response mapping
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设返回的JSON是任意的,那么最好的办法就是将其显示为字符串。为此,与其将JSON响应映射到POJO上,我建议将吊索模型调整为响应。
然后,您可以使用data-sly-use.model和吊索模型构造函数将吊索模型设置为视线代码中的模型,您可以将响应值设置为吊索模型的属性。
然后,您需要做的就是将该属性放入$ {}中,在瞄准镜HTML中。
如果JSON的格式/结构不是完全未知的,则可以在视线中使用Pojo。创建一些有条件测试POJO具有的属性,因此您可以将它们放入视力代码中。
Assuming the JSON returned is arbitrary, the best thing to do is simply display it as a string. To do that, instead of mapping the JSON response to a POJO I would recommend adapting a Sling model to the response.
Then, you can set that Sling model to be the model in your sightly code, using data-sly-use.model, and in the Sling model constructor you can set the response value to an attribute of the sling model.
Then all you'd need to do is put that attribute in a ${} in the sightly html.
If the format/structure of the JSON isn't completely unknown, you could use the POJO in the sightly. Create some conditionals to test what attributes the POJO has, so you can put them into the sightly code.
使用新版本的吊索模型,您可以通过指定资源类型和用于模型注释中的选择器来直接将模型作为servlet公开。将模型加载到Apache吊索中时,它会自动注册与模型相对应的Servlet,使您可以使用几乎零代码,创建一个servlet来访问模型的JSON表示。太酷了!
上述生活使您的生活更轻松!!
您可以在吊索模型中拥有所有对象。由于吊索模型充当servlet,因此您可以进行Ajax调用并获得实时响应。
请参考此文档。
https:// blog .perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
With the new version of Sling Models, you can directly expose a model as a Servlet by specifying a resource type and the selector to use in your model annotations. When the Model is loaded into Apache Sling, it automatically registers a Servlet corresponding to the model, allowing you to with nearly zero additional code, create a Servlet to access a JSON representation of the model. That’s super cool!
The above life makes your Life Easier!!
You can have all your objects in Sling Model. Since the sling model acts as a servlet You can make the AJAX call and get a real-time response.
Please refer to this document.
https://blogs.perficient.com/2018/07/26/no-servlets-required-exporting-data-with-sling-models/
正确的路径是:
因此,您必须提取将数据获取到OSGI服务中的代码。
但是,请保护调用外部API的代码。例如,如果外部API没有响应或极慢,则可以消耗AEM的所有可用线程。那么AEM可能完全无法使用。为了保护它,您可以用作信号量。
The correct path is:
So you have to extract the code that fetches the data into an OSGi service.
But please secure your code that calls the external API. As example if the External API is not responding or is extremely slow, it could consume all available threads of AEM. Then AEM could be completely unusable. To secure it, you could use as Example a Semaphore.