Expand my Community achievements bar.

My custom servlet is not being called

Avatar

Level 3

Hi,

 

I'm building a custom servlet that just output hello world. I bundled it and loaded the bundle into AEM, and bundle seems to be "active" without any errors.

Still, when I try to reach my servlet, I get 404.

 

This is my servlet code:

 

@component(
    service = Servlet.class, immediate = true,
    property = {
        "sling.servlet.methods=GET",
        "sling.servlet.paths=/bin/accessibility-issues-render"        
    }
)
public class AccessibilityIssuesServlet extends SlingSafeMethodsServlet {

private static final Logger LOG = 
 LoggerFactory.getLogger(AccessibilityIssuesServlet.class);

@Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {      
          LOG.info("AccessibilityIssuesServlet recieved GET: {}", request);
        }     
}

 

When I navigate to:
http://localhost:4502/bin/accessibility-issues-render

I get

Resource at '/bin/accessibility-issues-render' not found: No resource found

 

30 Replies

Avatar

Level 3

Hi,

 

Still struggling with this.

Happy to get any help on the topic.

 

Avatar

Level 4

hi @BenBr9 

 

Can you please post whole code along with import statements. 

 

Will re-check if any issue

Avatar

Level 3

Thanks!

 

package com.evinced.aem.core;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.nio.charset.StandardCharsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(
    service = Servlet.class, immediate = true,
    property = {
        "sling.servlet.methods=GET",
        "sling.servlet.paths=/bin/accessibility-issues-render"        
    }
)
public class AccessibilityIssuesServlet extends SlingSafeMethodsServlet {

    private static final String PUBLISH_HOST = "http://localhost:4503"; // URL of the publish instance
    private static final String TEMP_DIRECTORY = System.getProperty("java.io.tmpdir") + "/accessibility-data";
    private static final Logger LOG = LoggerFactory.getLogger(AccessibilityIssuesServlet.class);

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {        
        LOG.info("AccessibilityIssuesServlet recieved GET: {}", request);
....
....

Avatar

Level 4

There is no issue with the code. It should work as expected 

 

Update if any error in the logs

Avatar

Community Advisor

Sorry to hear that. I think at this point, you should start a new AEM instance, install a fresh project (not the one you're currently using), and make sure a demo servlet works. Then, slowly add your servlet to this project and see if the issue appears. Lastly, install your code project to see if the issue appears, this can give you some additional pointers. As others have already checked, it seems the servlet code itself is not the problem, so we are blindly advising on other aspects that we cannot see



Esteban Bustamante

Avatar

Level 2

Verify that the sling.servlet.paths property is configured to allow access at this path. AEM sometimes restricts access to paths under /bin in the author instance due to security. You may need to:

 

  • Go to UserAdmin in AEM (localhost:4502/useradmin).
  • Check permissions for the path /bin/accessibility-issues-render for your user or system user associated with this servlet.

Avatar

Level 3

This is what I see in the admin console. Not sure if this is ok or not.

BenBr9_0-1731058775746.png

 

 

/bin seems like a file there. It is strange.

But anyway, I also tried use /apps and it didn't work..

Avatar

Level 8

Hi @BenBr9,

sometimes when you are struggling with an issue for a long time, it makes sense to start from scratch.

What I would suggest is that you define your custom API path instead of relying on the OOTB /bin.

For example, you can add /rest to the org.apache.sling.api.servlets.ServletPathsServlet config.

Then try using that as a path in your servlet:

@Component(service = Servlet.class)
@SlingServletPaths(value = "/rest/test")

In case you are testing on Dispatcher, you might need to add:

 

/allowedPaths { 
    /rest/*
}

 

Avatar

Level 3

I tried to /rest path and it doesn't work.

 

How do I know if I'm using a dispatcher? Which file should I edit?

 

Avatar

Level 3

Found the issue!

 

My pom maven-bundle-plugin had these private packages configured:

 

<Private-Package> 
                            org.apache.httpcomponents.*,
                            org.apache.http.*,                        
                            com.google.gson.*,
                            com.google.gson.reflect.*,
                            com.google.gson.stream.*,
                            org.json.*,                          
                            org.opentest4j.*,
                            javax.servlet.*,
                            org.apache.sling.*,
                        </Private-Package>

 

I added it because of some bundle load errors. Seems I don't need this anymore, and when removing it - the servlet works!

 

Anyone knows why this configuration would cause issues?