Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

StackOverflowError when using adaptTo()

christianb34921
Level 1
Level 1

Hi all

I have a simple servlet which tries to adapt the SlingHttpServletRequest to an interface called TestEnvironment. The interface has one concrete implementation called ConcreteTestEnvironment which is annotated as sling model. When adapting the request I get a StackOverflowError. Do you know why? I'm using AEM 6.3 without service packs.

My versions are:

  • Apache Sling Models Implementation 1.3.9.r1784960
  • Apache Sling Models API 1.3.2

Here the code:

package ch.ti8m.ubs.fim.test;

import ch.ti8m.ubs.fim.test.models.TestEnvironment;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;

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

@Component(

   name = "ch.ti8m.ubs.fim.test.Servlet",
   metatype = true)

@Service(javax.servlet.Servlet.class)

@Properties({

   @Property(name = Constants.SERVICE_VENDOR, value = "ti8m"),
   @Property(name = Constants.SERVICE_DESCRIPTION, value = "Servlet"),
   @Property(name = "sling.servlet.methods", value = {"GET"}),
   @Property(name = "sling.servlet.paths", value = "/bin/fim/test/servlet"),
})

public class Servlet extends SlingAllMethodsServlet {

   @Override
   protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

   throws ServletException, IOException {

  TestEnvironment env = request.adaptTo(TestEnvironment.class);

   response.getWriter().write("Servlet called");
   }

}

1 Reply
christianb34921
Level 1
Level 1

Here the other classes. I cant edit my answer...

public interface TestEnvironment {

  SlingHttpServletRequest getRequest();
}

@Model(adaptables = SlingHttpServletRequest.class, adapters = TestEnvironment.class)

public class ConcreteTestEnvironment implements TestEnvironment {

   @Self
   SlingHttpServletRequest request;
   @Override
   public SlingHttpServletRequest getRequest() {

   return request;
   }

}