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

SlingHttpServletRequest request is null in slingmodel

  • August 12, 2019
  • 19 replies
  • 12883 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

Himanshu_Singhal
Community Advisor
Community Advisor
August 13, 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.

joerghoh
Adobe Employee
Adobe Employee
August 13, 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

arunpatidar
Community Advisor
Community Advisor
August 13, 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

Arun Patidar
DPrakashRaj
Community Advisor
Community Advisor
August 13, 2019

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

DPrakashRaj
Community Advisor
Community Advisor
August 13, 2019

already tried and still request is null

DPrakashRaj
Community Advisor
Community Advisor
August 13, 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

DPrakashRaj
Community Advisor
Community Advisor
August 13, 2019

Just for an update i am using aem 6.3.2.0

Himanshu_Singhal
Community Advisor
Community Advisor
August 13, 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.

DPrakashRaj
Community Advisor
Community Advisor
August 13, 2019

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