Expand my Community achievements bar.

SOLVED

Problem retrieving rendered JSON via SlingRequestProcessor in JSP

Avatar

Level 2
I have need to build a JSON representation of the content hierarchy represented in AEM.

I'm trying to use the SlingRequestProcessor to return the rendered JSON of a target page as a string I can use to insert into a larger JSON object.

I'm finding that the SlingRequestProcessor works as intended when retrieving the HTML representation of a page, but does not return anything when requesting a JSON response for the same page:

This example returns the HTML of the page correctly:

SlingRequestProcessor requestProcessor = sling.getService(SlingRequestProcessor.class); RequestResponseFactory requestResponseFactory = sling.getService(RequestResponseFactory.class); HttpServletRequest servrequest = requestResponseFactory.createRequest("GET", "/content/geometrixx-media/en/entertainment.html"); ByteArrayOutputStream servout = new ByteArrayOutputStream(); HttpServletResponse servresponse = requestResponseFactory.createResponse(servout); requestProcessor.processRequest(servrequest, servresponse, resourceResolver); String servhtml = servout.toString();

 

But the following example returns only an empty string:

 

SlingRequestProcessor requestProcessor = sling.getService(SlingRequestProcessor.class); RequestResponseFactory requestResponseFactory = sling.getService(RequestResponseFactory.class); HttpServletRequest servrequest = requestResponseFactory.createRequest("GET", "/content/geometrixx-media/en/entertainment.json"); ByteArrayOutputStream servout = new ByteArrayOutputStream(); HttpServletResponse servresponse = requestResponseFactory.createResponse(servout); requestProcessor.processRequest(servrequest, servresponse, resourceResolver); String servhtml = servout.toString();

 

I used the geometrixx site as an example, but in my real use-case the target page has a json.jsp in its component that creates a custom JSON response when accessed (eg. /content/geometrixx-media/en/entertainment/jcr:content.json)

I'm not sure why the above does not work for a JSON response.

1 Accepted Solution

Avatar

Correct answer by
Level 2

I eventually found the solution. It is impossible to return the json output of the content structure (eg. /content/geometrixx-media/en/entertainment.infinity.json) presumably because the response includes a application/json response header.

For my purposes I needed to target the output of my custom json.jsp in my content component (eg. /content/geometrixx-media/en/entertainment/jcr:content.json)

Originally my json.jsp included a response header to identify the response as JSON. Removing this header allowed the JSON to be properly returned via the requestResponseFactory. 

I think this confirms that requestResponseFactory specifically excludes responses based on the response header. I would consider this to be a bug as this prevents the requestResponseFactory from accessing the default JSON output of a node.

View solution in original post

6 Replies

Avatar

Administrator

Hi 

Please try it with "requestResponseFactory".

Please have a look ta this similar forum post :- 

Link:- http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

// How to read Content(.html or .json) from the content tree in .jsp files

I will try to get some internal comments on this as well.

~kautuk



Kautuk Sahni

Avatar

Level 2

I already looked at that post and the code shown above is essentially the same. At line 5 am I not using "requestResponseFactory" to create the request?

As I said in the original post, this works if the request works if the response returns HTML, but not JSON.

I also tried using a sling-include, but I need to have the response returned into a variable so that I can insert it into a larger JSON object.

Avatar

Administrator

Reply from internal expert:

You should make sure that json output is enabled in the Apache Sling GET Servlet config then also check if youhave any custom json.jsp handler scripts in their custom page component.

~kautuk



Kautuk Sahni

Avatar

Level 2

I have confirmed that JSON is enabled in the Apache Sling GET Servlet configuration. It still does not return JSON using the above method.

The page that I'm referencing does have a custom json.jsp in its component which is in fact the output I would like to return in the request, but even when I request the geometrixx page that does not have a custom json.jsp in it's component it still does not return JSON in the request. 

When I access the target page directly, the page returns the correct JSON and if I use a sling:include on the parent page it also returns the correct JSON, but I need to be able to return the JSON from the target page within my JSP file so that I can combine it with JSON from other pages.

Can the SlingRequestProcessor return the rendered JSON output of a page that has a custom json.jsp in it's component?

For example: /content/geometrixx-media/en/entertainment/jcr:content.json

Note that I'm requesting the JSON output of the jcr:conent of the page.

Avatar

Correct answer by
Level 2

I eventually found the solution. It is impossible to return the json output of the content structure (eg. /content/geometrixx-media/en/entertainment.infinity.json) presumably because the response includes a application/json response header.

For my purposes I needed to target the output of my custom json.jsp in my content component (eg. /content/geometrixx-media/en/entertainment/jcr:content.json)

Originally my json.jsp included a response header to identify the response as JSON. Removing this header allowed the JSON to be properly returned via the requestResponseFactory. 

I think this confirms that requestResponseFactory specifically excludes responses based on the response header. I would consider this to be a bug as this prevents the requestResponseFactory from accessing the default JSON output of a node.

Avatar

Level 3

@gregt65506620, I ran in to a similar issue.
i am calling /<path to compoonent>/_cq:dialog.infinity.json is returning empty.

Please share in details what needs to be changed in json.jsp