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

SlingHttpServletRequest request value is null while Injecting SlingHttpServletRequest to the sling model adaptable to Resource and SlingHttpServletRequest.

manasip
Level 2
Level 2

@Model(adaptables = {Resource.class, SlingHttpServletRequest.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL, resourceType = "reactaem/components/content/my-title")

@Exporter(name = "jackson", extensions = "json")

public class MyTitleModel {

private final Logger logger = LoggerFactory.getLogger(getClass());

@Inject

private String title;

@Inject

private String text;

@Inject

SlingHttpServletRequest slingRequest;

@PostConstruct

    public void init() {

if (slingRequest != null) {

String url = slingRequest.getRequestPathInfo().getResourcePath() ;

logger.info("URL=" + url);

}

else {

logger.info("SlingHttpServletRequest is null*****");

}

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getText() {

return text;

}

public void setText(String text) {

this.text = text;

}

}

7 Replies
Vijayalakshmi_S
Community Advisor
Community Advisor

Hi,

When the adaptable itself needs to be injected, we need to use an annotation named "@Self" along with "@Inject".

In this case, add self annotation to SlingHttpServletRequest.

See if it works with this.

Reference:

Apache Sling :: Sling Models

manasip
Level 2
Level 2

I am developing on AEM 6.3

I have already tried:

@Self

@Inject

private SlingHttpServletRequest slingRequest;

and

@SlingObject

private SlingHttpServletRequest slingRequest;

Any other way to get the SlingHttpServletRequest object?

sharathe9008234
Level 1
Level 1

Hi,
Is this issue resolved ?

I am also facing similar issue while exporting columnCotrol component.

Thanks!

Sharath

rajesh0926
Level 1
Level 1

Seems to be order of the adaptables does matter here

adaptables = {SlingHttpServletRequest.class, Resource.class }

Having sling request first in the list is solving the issue for me

carolinaguedes
Level 1
Level 1

hey, having the same problem. Any solution? Thank you