SlingHttpServletRequest returning null when using Jackson Exporter | Community
Skip to main content
Level 2
May 25, 2021
Solved

SlingHttpServletRequest returning null when using Jackson Exporter

  • May 25, 2021
  • 4 replies
  • 2694 views

Hi,

Im using SlingHttpServletRequest in my sling model class with jackson exporter. But the request object is always returning null. Can anyone help me on this.

 

@Model(adaptables = {SlingHttpServletRequest.class,Resource.class}, resourceType = { "sample/components/content/example_comp" }, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@3484101(name = "jackson", extensions = "json")
public class EESimWizardConfigModel {

 

@586265
SlingHttpServletRequest request;

 

@586265
private HashMap<String, String> productFamilyDetails;

 

public Map<String, String> getProductFamilyDetails() {
         if (request != null) {

             //then do something

         }

 

}

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 joerghoh

The problem is that you allow the model from being adapted from both the request and the resource, and on top of the injected request is optional.

That means, that it is very likely that the behind the scenes the adaption is not done by the request, but rather by the resource, and that the request field is not injected at all.

4 replies

Asutosh_Jena_
Community Advisor
Community Advisor
May 25, 2021

Hi @maheswariv26797 

 

Can you try with @SlingObject instead of @Inject once ?

 

@SlingObject

private SlingHttpServletRequest request;

 

http://www.sgaemsolutions.com/2017/06/sling-model-exporter-in-aem-63.html

 

 

Asutosh_Jena_
Community Advisor
Community Advisor
May 25, 2021

Hi @maheswariv26797 

 

I just tried with the below code and it works for me:

 

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import javax.inject.Named;

@Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, resourceType = {"weretail/components/content/heroimage"}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = "jackson", extensions = "json")
public class TestModel {

private static final Logger log = LoggerFactory.getLogger(TestModel.class);

@ValueMapValue
String buttonLinkTo;

@ValueMapValue
String fileReference;

@ValueMapValue
@Named("sling:resourceType")
String slingResourceType;

@SlingObject
private SlingHttpServletRequest request;

@PostConstruct
protected void init() {
log.info("***** :: init :: Start :: *****");
if (request != null) {
log.info("Request Path -> {}", request.getRequestPathInfo());
} else {
log.error("Request is NULL");
}
log.info("***** :: init :: End :: *****");
}

public String getButtonLinkTo() {
return buttonLinkTo;
}

public String getSlingResourceType() {
return slingResourceType;
}

public String getFileReference() {
return fileReference;
}
}

 

http://localhost:4502/content/we-retail/language-masters/en/experience/jcr:content/root/hero_image.model.json

 

Response I get:

{

  • buttonLinkTo: null,
  • fileReference: "/content/dam/we-retail/en/activities/hiking-camping/trekker-ama-dablam.jpg",
  • slingResourceType: "weretail/components/content/heroimage"

} 

 

Log:

25.05.2021 22:05:15.711 *INFO* [[0:0:0:0:0:0:0:1] [1621960515699] GET /content/we-retail/language-masters/en/experience/jcr:content/root/hero_image.model.json HTTP/1.1] com.community.core.models.TestModel ***** :: init :: Start :: *****
25.05.2021 22:05:15.711 *INFO* [[0:0:0:0:0:0:0:1] [1621960515699] GET /content/we-retail/language-masters/en/experience/jcr:content/root/hero_image.model.json HTTP/1.1] com.community.core.models.TestModel Request Path -> SlingRequestPathInfo: path='/content/we-retail/language-masters/en/experience/jcr:content/root/hero_image', selectorString='model', extension='json', suffix='null'
25.05.2021 22:05:15.711 *INFO* [[0:0:0:0:0:0:0:1] [1621960515699] GET /content/we-retail/language-masters/en/experience/jcr:content/root/hero_image.model.json HTTP/1.1] com.community.core.models.TestModel ***** :: init :: End :: *****

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
May 25, 2021

The problem is that you allow the model from being adapted from both the request and the resource, and on top of the injected request is optional.

That means, that it is very likely that the behind the scenes the adaption is not done by the request, but rather by the resource, and that the request field is not injected at all.

Level 4
May 26, 2021

r

Level 2
May 26, 2021

@maheswariv26797Can you please try @1961677 private SlingHttpServletRequest request; annotation like this.