Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

Writing a custom json servlet

Avatar

Level 5

I'm struggling to override the default out of the box json servlet. I want to create my own for a specific resource type and when a specific selector is used. However it seems to keep calling the default. To be clear I want to keep the default servlet in all other scenarios. My annotations for the custom servlet are:

@SlingServlet(methods = {HttpConstants.METHOD_GET},
   metatype = true,
   resourceTypes = {"wtr/components/structure/offertypepage"},
   selectors = {"mapper"},
   extensions = {"json"})

and if i call this servlet e.g. /path/to/resource.mapper.json I get an error from the default json servlet complaining that mapper is not a valid selector. IF i switch the extension to html then it works fine...but obviously returning json from .html is not ideal. Also if i called /path/to/resource.2.json then I'd like the normal ootb json servlet to work (ideally).

2 Replies

Avatar

Level 10

Looks like you are binding your Servlet by Resource type. In our article on this subject - this works:

@SlingServlet(

    methods = {"GET"},

    metatype = true,

    resourceTypes = {"services/powerproxy"},

    selectors = {"groups"})

Make sure you cover everything we should here: Adobe Experience Manager Help | Binding Adobe Experience Manager Servlets to ResourceTypes

Avatar

Level 4

Hi Sutty100

The order of selectors calling is as follows:

- sling:resourceType is hr/jobs then it will be looking at /apps/hr/jobs/jobs.POST.esp

- /content/corporate/jobs/developer.pdf  will be looked at /apps/hr/jobs/jobs.pdf.esp

- if no sling resourcetype is present then:

- /content/corporate/jobs/developer.html will be looked at /apps/content/corporate/jobs/

- if not then default folder /libs will be looked for rendering

What you need to do is now create a new selector file at /apps/wtr/components/structure/offertypepage.json.esp It should work with your JSON extension.

Hope this helps.