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 request is null in slingmodel

Avatar

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile
DPrakashRaj
Level 2

12-08-2019

i want to adapt to sling model from another sling model. While doing so the SlingHttpServletRequest request is always null in second sling model. i tried with @Inject, @self and @@SlingObject. I am able to get request in FirstModel but not in SecondModel, i believe this happens because SecondModel is getting adapted with resource. Had anyone faced this issue and  how it worked..

Thanks in advance

basically my code is simlilar to:

@Model(

        adaptables =  {SlingHttpServletRequest.class, Resource.class},

        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL

)

public class SecondModel {

     @Inject

     private SlingHttpServletRequest request;

}

@Model(

        adaptables =  SlingHttpServletRequest.class,

        resourceType = "mycomponent path",

        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL

)

public class FirstModel {

@Inject

private SlingHttpServletRequest request;

@PostConstruct

    public void initResource() {

          SecondModel  obj  =  request.getResourceResolver().getResource(resourcePath).adaptTo(SecondModel .class)

     }

}

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,112 likes

Total Posts

3,145 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,112 likes

Total Posts

3,145 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

13-08-2019

The problem is that the SlingModel AdapterFactory cannot do the adaption properly, because it does not know about the details of FirstModel to inject the request into the SecondModel.

Either you adapt the request directly to SecondModel or you write a custom AdapterFactory which can the proper adaption from FirstModel to SecondModel. This is indepdent of the AEM version you use.

Answers (18)

Answers (18)

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

13-08-2019

you can try with the approach suggested by @Jörg Hoh

Avatar

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile
DPrakashRaj
Level 2

13-08-2019

Yes, request is available in FirstModel and also adaptTo() works. My problem is that i have

@SlingObject

    private SlingHttpServletRequest request;

also in SecondModel. This secondModel class uses request object to get requestURl from request and italways remains null. So, if i have above requirement then how can in inject request in SecondModel

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

13-08-2019

Hi I tried the example whicj I shared with you earlier it works for me.

Performing Sling Model Adaptation Using Request and Resource Objects

@Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public class FirstModel {

@SlingObject

    private SlingHttpServletRequest request;

@PostConstruct

    public void initResource() {

          SecondModel  obj  =  request.getResourceResolver().getResource(resourcePath).adaptTo(SecondModel .class)

     }

}

With this approach request object should be available in FirstModel but not in SecondModel.

You should check the imports if doesn't work.

But if resource.getResourceResolver().getResource(resourcePath).adaptTo(SecondModel .class) works, you can use it.

Avatar

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile
DPrakashRaj
Level 2

13-08-2019

resource.getResourceResolver().getResource(resourcePath).adaptTo(SecondModel .class) works but request object in FirstModel always remain null.

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

13-08-2019

If it doesn't work, you can get resource using resource itself.

@Model(adaptables = Resource.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

public class SecondModel {

@Self

Resource resource;

@PostConstruct

    public void initResource() {

          SecondModel  obj  =  resource.getResourceResolver().getResource(resourcePath).adaptTo(SecondModel .class)

     }

}

Avatar

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile
DPrakashRaj
Level 2

13-08-2019

i tried this code but it's not working, possible because i am using 6.3.2.0 and the example is for aem 6.4. Not sure if this is the reason

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

13-08-2019

Avatar

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile
Himanshu_Singhal
MVP

13-08-2019

adapTo() only accept adapting class name. If you need to adapt to request, the link I shared in 1st reply describes how can you create custom adaptorfactory to adapt to request instead of resource.

https://taradevko.com/aem/sling-model-request-parameter-injector/

Avatar

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile
DPrakashRaj
Level 2

13-08-2019

Not Sure if adapTo() accept any parameter other than class name. if you have any reference doc that will be helpful

Avatar

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile
Himanshu_Singhal
MVP

13-08-2019

In that case, one way is instantiating the model class by creating object and passing request (via constructor or setter). That's the traditional Java way of handling things.

Otherwise, you'll have to write custom AdaptorFactory.

Avatar

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile
DPrakashRaj
Level 2

13-08-2019

Just for an update i am using aem 6.3.2.0

Avatar

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile
DPrakashRaj
Level 2

13-08-2019

it's not failing but request comes out to be null and my requirement is to get complete Url of the page in sling model class

Avatar

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile
DPrakashRaj
Level 2

13-08-2019

already tried and still request is null

Avatar

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile

Avatar
Boost 5
Level 2
DPrakashRaj
Level 2

Likes

5 likes

Total Posts

25 posts

Correct Reply

0 solutions
Top badges earned
Boost 5
Boost 3
Boost 1
View profile
DPrakashRaj
Level 2

13-08-2019

With request.adaptTo() throws error with complaining unable to bind the injected value

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,348 likes

Total Posts

3,226 posts

Correct Reply

916 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

13-08-2019

Please inject using SlingObject annotation.

@SlingObject

    private SlingHttpServletRequest request;

Injects commonly used sling objects if the field matches with the class: request, response, resource resolver, current resource, SlingScriptHelper

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,112 likes

Total Posts

3,145 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,112 likes

Total Posts

3,145 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

12-08-2019

Hi,

can you show the code you are using to adapt FirstModel to SecondModel? My impression is that the (default) AdapterFactory which is behind these adaptions for the SlingModel cannot handle that case, but because the Request injection is marked as optional, it does not fail.

If you want to handle that case of adapting FirstModel to SecondModel, it's probably best to write a custom AdapterFactory.

Jörg

Avatar

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile

Avatar
Validate 25
MVP
Himanshu_Singhal
MVP

Likes

65 likes

Total Posts

206 posts

Correct Reply

23 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Give Back 5
Give Back 3
View profile
Himanshu_Singhal
MVP

12-08-2019

You may try to adaptTo() another Model with request instead of resource and check it that works. Never tried adapting with request but it's just a thought.

https://taradevko.com/aem/sling-model-request-parameter-injector/  - This article might help as fits your scenario.

Avatar

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile

Avatar
Establish
MVP
Gaurav-Behl
MVP

Likes

248 likes

Total Posts

1,147 posts

Correct Reply

283 solutions
Top badges earned
Establish
Give back 300
Give Back 50
Give Back 5
Give Back 3
View profile
Gaurav-Behl
MVP

12-08-2019