Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 6

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.

1 Accepted Solution

Avatar

Correct answer by
Employee
6 Replies

Avatar

Level 10

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

Avatar

Level 10

We will release more Sling Model examples as well.

Avatar

Employee

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.

Avatar

Level 6

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?

Avatar

Correct answer by
Employee

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

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

Avatar

Level 6

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.