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
BedrockMission!

Learn More

View all

Sign in to view all badges

SlingHttpServletRequest returning null when using Jackson Exporter

Avatar

Avatar
Validate 1
Level 1
maheswariv26797
Level 1

Likes

0 likes

Total Posts

16 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
View profile

Avatar
Validate 1
Level 1
maheswariv26797
Level 1

Likes

0 likes

Total Posts

16 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
View profile
maheswariv26797
Level 1

25-05-2021

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

         }

 

}

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,114 likes

Total Posts

3,148 posts

Correct Reply

1,072 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,114 likes

Total Posts

3,148 posts

Correct Reply

1,072 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

25-05-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.

Answers (1)

Answers (1)

Avatar

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

559 likes

Total Posts

676 posts

Correct Reply

194 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile

Avatar
Give Back 100
Level 10
asutosh_jena
Level 10

Likes

559 likes

Total Posts

676 posts

Correct Reply

194 solutions
Top badges earned
Give Back 100
Boost 500
Affirm 100
Ignite 1
Establish
View profile
asutosh_jena
Level 10

25-05-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

 

 

maheswariv26797

Hi @asutosh_jena,

 

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

 

Thanks,

Maheswari

asutosh_jena

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