Expand my Community achievements bar.

SOLVED

Registering custom servlet using sling resource type

Avatar

Level 4

Hey there ,

  I'm trying to  registering a custom search servlet by using sling resource type. Deployment of bundle and registering of service is fine as I can view them in felix console . 

My servlet is annotated as follows 

case 1 : @org.apache.felix.scr.annotations.Component(metatype = false) @Service(Servlet.class) @Properties({         @Property(name = "sling.servlet.extensions", value = "html"),         @Property(name = "sling.servlet.selectors", value = "jsonsearch"),         @Property(name = "sling.servlet.resourceTypes", value = "geometrixx/components/homepage"),         @Property(name = "sling.servlet.methods", value = "GET") }) public class SearchJsonServlet extends SlingSafeMethodsServlet {

The problem  is that this service is not getting registered for the specified resource type  , From the resource Servlet resolver in felix console  ,  I can see that DefaultGetServlet is getting picked up .  If in the browser I try to open the page http://localhost:4502/content/geometrixx/en.jsonsearch.html  default getservlet is getting picked up instead of the custom search servlet .( To confirm the page is backed with resource type 'geometrixx/components/homepage' . )

 

I got my custom servlet to be picked up by updating the annotations to below ,  but this would change the global behaviour which I want to avoid , 

case 2 : 

@org.apache.felix.scr.annotations.Component(metatype = false)
@Service(Servlet.class)
@Properties({
        @Property(name = "sling.servlet.extensions", value = "html"),
        @Property(name = "sling.servlet.selectors", value = "jsonsearch"),
        @Property(name = "sling.servlet.resourceTypes", value = "sling/servlet/default"),
        @Property(name = "sling.servlet.methods", value = "GET")
})

 

Could any one provide me with some pointers to get it done in the right way ? 

 

Thanks in advance

 

 

 

'

 

 

 

 

 

The block I'm getting is  ,

1 Accepted Solution

Avatar

Correct answer by
Level 2

One of the weird constraints with Sling Servlets is that they actually require a name to be specified to be registered properly:

http://labs.sixdimensions.com/blog/dan-klco/2012-12-01/gotcha-sling-servlet-requires-name

If you update your @Component annotation to have a name attribute, it should register properly as a Servlet.

View solution in original post

1 Reply

Avatar

Correct answer by
Level 2

One of the weird constraints with Sling Servlets is that they actually require a name to be specified to be registered properly:

http://labs.sixdimensions.com/blog/dan-klco/2012-12-01/gotcha-sling-servlet-requires-name

If you update your @Component annotation to have a name attribute, it should register properly as a Servlet.