Expand my Community achievements bar.

SOLVED

SlingHttpServletRequest returning null when using Jackson Exporter

Avatar

Level 2

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)
@exporter(name = "jackson", extensions = "json")
public class EESimWizardConfigModel {

 

@inject
SlingHttpServletRequest request;

 

@inject
private HashMap<String, String> productFamilyDetails;

 

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

             //then do something

         }

 

}

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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.

View solution in original post

6 Replies

Avatar

Community Advisor

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

 

 

Avatar

Level 2

Hi @Asutosh_Jena_,

 

yes,i have used @SlingObject but still it did not work. Am i missing anything?

 

Thanks,

Maheswari

Avatar

Community Advisor

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.m...

 

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 :: *****

Avatar

Correct answer by
Employee Advisor

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.

Avatar

Level 2

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

Raushan_0-1622008269078.png