Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

OSGI Servlet not found when I hit the servlet path

Avatar

Level 3

Hi All,

I'm just replacing the felix annotations with OSGi DS annotations for my servlet and I could able to build the project success. Once I deploy the bundle and hit the servlet path I'm getting 404 response in the page and the error log I'm getting this org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Resource /bin/custom/sample not found.

Thing I have tried:

  • Tried with uber 6.3.0, 6.4.0 & 6.4.1
  • tried with Maven-bundle-plugin version 3.2.0 & 3.3.0

I checked my servlet is active and I'm having the required entries in pom.xml. But when I compared the servlet which is used Felix and OSGI annotation I could not see the Reference package in OSGI servlet. Below are the screen shot of felix & OSGi servlet. Further what I should check? Appreciate your help.

Felix Servlet:

1545048_pastedImage_7.png

OSGI Servlet:

1545049_pastedImage_8.png

Regards,

Vijay

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

Indeed it's quite interesting, that the Reference went missing. Can you check in the OSGI-INF/ directory inside your bundle if the reference is actually defined in the XML? This helps to find out if the annotation is working properly or if it's a runtime issue.

Jörg

View solution in original post

10 Replies

Avatar

Community Advisor

Hi,

Seems issue with the servlet code?

you can check sling servlet resolver http://localhost:4504/system/console/servletresolver

I've created a simple servlet, you can check and try

aem63app-repo/SimpleGetGroup.java at master · arunpatidar02/aem63app-repo · GitHub

Thanks

Arun



Arun Patidar

Avatar

Level 10

Try the sample that Arun suggested. Let us know if you still have an issue.

Avatar

Level 10

Watch this video (a servlet being successfully invoked using AJAX and GET). As discussed in the video - try shorting the path - ie bin/customservlet.

Avatar

Level 10

Hi Siva,

Yes look into that sample of Arun pointed out.

And also, please look into the video shown by Scott. It will brief you more clearly.

Hope this helps!!

Thanks,

Ratna Kumar.

Avatar

Level 3

Hi Arun,

Thanks for you help. I have my servlet code in place as you suggested already. For your reference I have placed the Felix annotation Servlet and OSGi DS annotations Servlet code below. Request you to help me if I missed anything here.

Felix Annoattions:

import ch.sample.aem.core.constants.AemConstants;

import ch.sample.aem.core.dto.SystemStatusBean;

import ch.sample.aem.core.helper.SystemStatusHelper;

import com.google.gson.Gson;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Reference;

import org.apache.felix.scr.annotations.Service;

import org.apache.felix.scr.annotations.sling.SlingServlet;

import org.apache.jackrabbit.vault.packaging.Packaging;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.osgi.service.component.ComponentContext;

import org.osgi.service.component.annotations.Component;

@Service

@SuppressWarnings("serial")

@SlingServlet(paths = "/bin/custom/samplesystemstatus")

public class SampleSystemStatus extends SlingSafeMethodsServlet {

  /**

   * This is created to invoke the sample bundle, package and server details in AEM.

   *

   * @return jsonString

   */

  @Reference

  private Packaging packaging;

  SystemStatusHelper systemStatusHelper;

  SystemStatusBean systemStatus;

  protected void activate(ComponentContext ctx) {

    systemStatusHelper = new SystemStatusHelper();

    systemStatus = new SystemStatusBean();

    systemStatus = new SystemStatusBean();

    systemStatusHelper.getBundlesList(ctx, systemStatus);

  }

  @Override

  protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)

      throws ServletException, IOException {

    systemStatusHelper.getHostDetails(req, systemStatus);

    systemStatus.setAlive(Boolean.TRUE);

    systemStatusHelper.getPackageList(req, systemStatus, packaging);

    String systemStatusJsonStr = new Gson().toJson(systemStatus);

    resp.setContentType(AemConstants.CONTENTTYPE);

    PrintWriter out = resp.getWriter();

    out.println(systemStatusJsonStr);

  }

}

After changed to OSGi Annotations:

package ch.sample.aem.core.servlet;

import ch.sample.aem.core.constants.AemConstants;

import ch.sample.aem.core.dto.SystemStatusBean;

import ch.sample.aem.core.helper.SystemStatusHelper;

import com.google.gson.Gson;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import org.apache.jackrabbit.vault.packaging.Packaging;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.osgi.service.component.ComponentContext;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

@SuppressWarnings("serial")

@Component(service = Servlet.class, immediate = true,

    property = {

        "sling.servlet.paths =  /bin/custom/samplesystemstatus",          

       

        "sling.servlet.methods=GET"

})

public class SampleSystemStatus extends SlingSafeMethodsServlet {

  /**

   * This is created to invoke the sample bundle, package and server details in AEM.

   *

   * @return jsonString

   */

  @Reference

  private Packaging packaging;

  SystemStatusHelper systemStatusHelper;

  SystemStatusBean systemStatus;

  protected void activate(ComponentContext ctx) {

    systemStatusHelper = new SystemStatusHelper();

    systemStatus = new SystemStatusBean();

    systemStatusHelper.getBundlesList(ctx, systemStatus);

  }

  @Override

  protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)

      throws ServletException, IOException {

    systemStatusHelper.getHostDetails(req, systemStatus);

    systemStatus.setAlive(Boolean.TRUE);

    systemStatusHelper.getPackageList(req, systemStatus, packaging);

    String systemStatusJsonStr = new Gson().toJson(systemStatus);

    resp.setContentType(AemConstants.CONTENTTYPE);

    PrintWriter out = resp.getWriter();

    out.println(systemStatusJsonStr);

  }

}

Since I'm unable to replace the @Reference annotation directly as recommend here https://blog.osoco.de/2016/05/migrating-from-the-apache-felix-scr-annotations-to-the-osgi-declarativ...  so I have still used Felix @Refernce. Appreciate your help.

smacdonald2008Ratna Kumardgordon86

Regards,

Vijay

Avatar

Level 10

Did you watch the video i posted.

Avatar

Correct answer by
Employee Advisor

Indeed it's quite interesting, that the Reference went missing. Can you check in the OSGI-INF/ directory inside your bundle if the reference is actually defined in the XML? This helps to find out if the annotation is working properly or if it's a runtime issue.

Jörg

Avatar

Level 3

Thanks Jorg. Finally I fixed the issue through .xml which has genretaed by my servlet. In XML I found out the space has been appended with my servlet path and that's causing an issue. Now removed the space and the servlet is getting called now.

Also If I add the below plugin then the XML is generated for my servlet otherwise it didn't.

<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId><version>3.5.1</version> <extensions>true</extensions> <executions> <!-- Configure extra execution of 'manifest' in process-classes phase to make sure SCR metadata is generated before unit test runs --> <execution> <id>scr-metadata</id> <goals> <goal>manifest</goal> </goals> <configuration><supportIncrementalBuild>true</supportIncrementalBuild> </configuration> </execution> </executions><configuration> <exportScr>true</exportScr> <instructions> <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName> <Bundle-Description>${project.description} (Rev. ${buildNumber} at ${timestamp})</Bundle-Description> <!-- Enable processing of OSGI DS component annotations --> <_dsannotations>*</_dsannotations> <!-- Enable processing of OSGI metatype annotations --> <_metatypeannotations>*</_metatypeannotations> <_plugin> <!-- Support parsing of maven-scr-plugin annotations through the felix.scr.bnd plugin -->org.apache.felix.scrplugin.bnd.SCRDescriptorBndPlugin, <!-- Generate bundle header containing all configuration annotation classes --> org.apache.sling.caconfig.bndplugin.ConfigurationClassScannerPlugin, <!-- Generate bundle header for Sling Models classes --> org.apache.sling.bnd.models.ModelsScannerPlugin </_plugin></instructions> </configuration> <dependencies> <dependency> <groupId>org.apache.felix</groupId><artifactId>org.apache.felix.scr.bnd</artifactId> <version>1.9.0</version> </dependency> <dependency><groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.scr.generator</artifactId><version>1.18.0</version> </dependency> <dependency> <groupId>org.apache.felix</groupId><artifactId>org.apache.felix.scr.annotations</artifactId> <version>1.11.0</version> </dependency> <dependency><groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.caconfig.bnd-plugin</artifactId><version>1.0.2</version> </dependency> <dependency> <groupId>org.apache.sling</groupId><artifactId>org.apache.sling.bnd.models</artifactId> <version>1.0.0</version> </dependency> </dependencies></plugin>

Regards,

Vijay

Avatar

Level 1

Hii sivas61374651,

I was also facing the same issue, pls do help me for resolving this issue...

Avatar

Level 3

Thanks all for you help here. You guys are really great. smacdonald2008Arun PatidarRatna Kumar