SlingHttpServletRequest request is null in slingmodel | Community
Skip to main content
DPrakashRaj
New Participant
August 12, 2019
Solved

SlingHttpServletRequest request is null in slingmodel

  • August 12, 2019
  • 19 replies
  • 12841 views

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)

     }

}

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

19 replies

arunpatidar
New Participant
August 13, 2019

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

Arun Patidar
DPrakashRaj
New Participant
August 13, 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

arunpatidar
New Participant
August 13, 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.

Arun Patidar
DPrakashRaj
New Participant
August 13, 2019

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

arunpatidar
New Participant
August 13, 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)

     }

}

Arun Patidar
joerghoh
joerghohAccepted solution
Employee
August 13, 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.

DPrakashRaj
New Participant
August 13, 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

arunpatidar
New Participant
August 13, 2019
Himanshu_Singhal
New Participant
August 13, 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/

DPrakashRaj
New Participant
August 13, 2019

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