Skip to main content
Level 2
Nov 4, 2024 1:24 PM UTC

My custom servlet is not being called

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:

 

@8220494( 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

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

30 replies

SureshDhulipudi
Community Advisor
Community Advisor
Nov 4, 2024 2:02 PM UTC

Please check the error log for more details.

 

Change LogFactory initiation.

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

 

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;
import java.io.IOException;

@8220494(
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);

@9944223
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
LOG.info("AccessibilityIssuesServlet received GET: {}", request);
response.getWriter().write("Servlet is working");
}
}

Translate
BenBr9Author
Level 2
Nov 5, 2024 8:19 AM UTC

Hi

 

There are no errors in the errors.log  it only state that my servlet has been registered, my bundle is active, but it is not getting called.

Translate
EstebanBustamante
Adobe Champion and Community Advisor
Adobe Champion and Community Advisor
Nov 4, 2024 4:19 PM UTC

Hi, 

Pls check if the bundle is active and the servlet is correctly exposed. 

Translate
Esteban Bustamante
BenBr9Author
Level 2
Nov 5, 2024 8:21 AM UTC

The bundle is indeed active and correctly exposed..

 

Translate
daniel-strmecki
Adobe Champion, Community Advisor, UG Leader
Adobe Champion, Community Advisor, UG Leader
Nov 4, 2024 10:16 PM UTC

Hi @benbr9,

check the OSGi Configuration named Apache Sling Servlet/Script Resolver and Error Handler if it allows for the /bin/* path. By default /bin/* should be allowed, but maybe you are not using the default config. Also, it is recommended to use resource type based servlets over path based.

 

Good luck,

Daniel

Translate
PRATHYUSHA_VP
Community Advisor
Community Advisor
Nov 5, 2024 7:03 AM UTC

Additionally you can check servlet path in below sling servlet resolver  for additional debugging purpose -

 

http://localhost:4502/system/console/servletresolver

 

Hope this helps 

 

Thanks

Translate
Prathyusha - AEM Developer
GabrielMircea
Level 2
Nov 5, 2024 1:42 AM UTC

In Sling, if you access a servlet that has been mapped to a path, it will only respond if there is no other resource at that path. If there's a resource with the same path, the servlet won't be invoked.

 

Check the CRXDE Lite (http://localhost:4502/crx/de) to ensure there isn't an existing resource at /bin/accessibility-issues-render.

Translate
BenBr9Author
Level 2
Nov 5, 2024 8:24 AM UTC

I checked crx/de and there is no existing resource under /bin

 

 

Translate
kautuk_sahni
Level 9
Nov 5, 2024 1:57 PM UTC

@benbr9 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Translate
Kautuk Sahni
BenBr9Author
Level 2
Nov 5, 2024 2:09 PM UTC

Didn't find an answer yet, no

Translate
chavad
Level 2
Nov 5, 2024 5:39 PM UTC

Hi @benbr9 

Can you check in http://localhost:4502/system/console/components your servlet is there or not?

Translate
BenBr9Author
Level 2
Nov 6, 2024 9:02 AM UTC

Yes it's there, and it is active

 

 

Translate
BenBr9Author
Level 2
Nov 8, 2024 7:13 AM UTC

Hi,

 

Still struggling with this.

Happy to get any help on the topic.

 

Translate
PRATHYUSHA_VP
Community Advisor
Community Advisor
Nov 8, 2024 8:49 AM UTC

hi @benbr9 

 

Can you please post whole code along with import statements. 

 

Will re-check if any issue

Translate
Prathyusha - AEM Developer
BenBr9Author
Level 2
Nov 8, 2024 9:07 AM UTC

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); @9944223 protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException { LOG.info("AccessibilityIssuesServlet recieved GET: {}", request); .... ....
Translate
GabrielMircea
Level 2
Nov 8, 2024 9:23 AM UTC

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.
Translate
BenBr9Author
Level 2
Nov 8, 2024 9:39 AM UTC

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

 

 

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

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

Translate
daniel-strmecki
Adobe Champion, Community Advisor, UG Leader
Adobe Champion, Community Advisor, UG Leader
Nov 8, 2024 10:47 AM UTC

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/* }

 

Translate
BenBr9Author
Level 2
Nov 10, 2024 9:22 AM UTC

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?

 

Translate
BenBr9Author
Level 2
Nov 10, 2024 9:44 AM UTC

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?

Translate