AEM 6.3 sling models implementation Error: cannot be correctly instantiated by the Use API? | Community
Skip to main content
July 11, 2017
Solved

AEM 6.3 sling models implementation Error: cannot be correctly instantiated by the Use API?

  • July 11, 2017
  • 6 replies
  • 6624 views

I try to write a class called Article.java.

if I do this

@Model(adaptables=Resource.class)

public class Article {

@Inject @Default(values="")

    private String title;

}

it works fine by retrieving from the article.html using <a data-sly-use.article="mycomponents.Article">

However, try to following the acs-aem sample code to implement an interface called Article, and another class called ArticleImpl as below

@Model(adaptables = SlingHttpServletRequest.class, adapters = Article.class, resourceType = ArticleImpl.RESOURCE_TYPE)

@Exporter(name = Constants.EXPORTER_NAME, extensions = Constants.EXPORTER_EXTENSION)

public class ArticleImpl implements Article {

    @ValueMapValue(optional = true, name = "title")

      private String title;

}

When trying to retrieve the title from article component, I am getting the error below

Java.lang.NoSuchMethodException: mycomponents.Article.<init>() (in this implementation, mycomponents.Article is an interface)

An InstatiationException issue.

I don't see the sample code to create a default constructor.

Please help.

Thanks.

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

I made an example too, can you check and compare?

htl-examples/TitleImpl.java at master · heervisscher/htl-examples · GitHub

6 replies

smacdonald2008
July 11, 2017

See this article - it has an Sling Model example and shows how to display the value in a front end -- Developing your first Experience Manager 6.3 Components

smacdonald2008
July 11, 2017

We will release more Sling Model examples as well.

Feike_Visser1
Adobe Employee
Adobe Employee
July 12, 2017

Looking at your sample you are using SlingHttpServletRequest.class in your second example.

Can you compare your code with the core components? They are using this pattern too.

BigT168Author
July 12, 2017

I follow the core components as example.

However even the first example, I have to add below codes in the pom.xml to make it work

<Import-Package>

                            javax.inject;version=1,

                            javax.annotation;version=0.0.0.1_008_JavaSE,

                            <!-- JEE Servlet -->

                            javax.servlet,

                            javax.servlet.http,

                            <!-- Sling -->

                            org.apache.sling.api.resource;version=2.10.0,

                            org.apache.sling.models.annotations;version=1.4.0,

                            org.apache.sling.models.annotations.injectorspecific;version=1.1.0,

                            com.adobe.cq.sightly;version=3.1.0,

                            org.apache.commons.lang3; version=2.5,

                            !*

</Import-Package>

It doesn't look right, but it works for the first example.

Any ideas?

Feike_Visser1
Adobe Employee
Feike_Visser1Adobe EmployeeAccepted solution
Adobe Employee
July 12, 2017

I made an example too, can you check and compare?

htl-examples/TitleImpl.java at master · heervisscher/htl-examples · GitHub

BigT168Author
July 12, 2017

Thanks, working now.

I think the <import-package> lines, somehow messed up with the result. I removed the lines, now it works fine.

By the way, can you explain when to use

@Model(adaptables=Resource.class)   vs.

@Model(adaptables = SlingHttpServletRequest.class, adapters = Article.class, resourceType = ArticleImpl.RESOURCE_TYPE)

If my article component has an image, how to reuse the core image class (ImageImpl.java)? so I can pass the image path and return me a smart image class.

Do you have an example?

Thanks a lot.