AEM 6.3 sling models implementation Error: cannot be correctly instantiated by the Use API? | Community
Skip to main content
Level 5
Jul 11, 2017 7:23 PM UTC
Solved

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

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.
Feike_Visser1
Feike_Visser1Accepted solution
Employee
Jul 12, 2017 2:16 PM UTC

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

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

6 replies

BigT168Author
Level 5
Jul 12, 2017 8:45 PM UTC

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.

Translate
Feike_Visser1
Feike_Visser1Accepted solution
Employee
Jul 12, 2017 2:16 PM UTC

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

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

Translate
BigT168Author
Level 5
Jul 12, 2017 2:00 PM UTC

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?

Translate
Feike_Visser1
Employee
Jul 12, 2017 7:16 AM UTC

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.

Translate
smacdonald2008
Level 10
Jul 11, 2017 7:59 PM UTC

We will release more Sling Model examples as well.

Translate
smacdonald2008
Level 10
Jul 11, 2017 7:58 PM UTC

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

Translate