OSGI Servlet not found when I hit the servlet path | Community
Skip to main content
Level 3
Aug 9, 2018 7:48 AM UTC
Solved

OSGI Servlet not found when I hit the servlet path

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:

OSGI Servlet:

Regards,

Vijay

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
joerghoh
joerghohAccepted solution
Employee
Aug 9, 2018 7:57 PM UTC

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

10 replies

Level 3
Aug 13, 2018 10:24 AM UTC

Thanks all for you help here. You guys are really great. smacdonald2008​ Arun Patidar​ Ratna Kumar​

Translate
Level 3
Aug 13, 2018 10:20 AM UTC

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

Translate
New Participant
Jul 31, 2023 1:01 PM UTC

Hii sivas61374651,

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

Translate
joerghoh
joerghohAccepted solution
Employee
Aug 9, 2018 7:57 PM UTC

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

Translate
smacdonald2008
Level 10
Aug 9, 2018 5:11 PM UTC

Did you watch the video i posted.

Translate
Level 3
Aug 9, 2018 4:08 PM UTC

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-declarative-services-annotatio…  so I have still used Felix @Refernce. Appreciate your help.

smacdonald2008​ Ratna Kumar​ dgordon86​

Regards,

Vijay

Translate
Ratna_Kumar
Level 10
Aug 9, 2018 12:34 PM UTC

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.

Translate
smacdonald2008
Level 10
Aug 9, 2018 12:28 PM UTC

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

Translate
smacdonald2008
Level 10
Aug 9, 2018 11:19 AM UTC

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

Translate
arunpatidar
New Participant
Aug 9, 2018 9:05 AM UTC

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

Translate
Arun Patidar