-1

I have the following code:

package com.example.helloworld;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.annotation.RequestScope;

@RestController
public class HelloWorldController {
  @GetMapping("/")
  public ResponseEntity<String> getGet(
      @Autowired Foo foo) {

    foo.sayHi();

    return new ResponseEntity<>("OK", HttpStatus.OK);
  }
}

@Component
@RequestScope
class Foo {

  private @Autowired HttpServletRequest request;

  public void sayHi() {
    var name = this.request.getHeader("x-user-name");
    System.out.println("Hi " + name);
  }
}

When I try to curl with curl -H x-user-name:capybara http://localhost:8080, the following error is produced:

2022-09-09 17:07:28.623 ERROR 26350 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException: Cannot invoke "javax.servlet.http.HttpServletRequest.getHeader(String)" because "this.request" is null] with root cause

java.lang.NullPointerException: Cannot invoke "javax.servlet.http.HttpServletRequest.getHeader(String)" because "this.request" is null
        at com.example.helloworld.Foo.sayHi(HelloWorldController.java:32) ~[classes!/:0.0.1-SNAPSHOT]
        at com.example.helloworld.HelloWorldController.getGet(HelloWorldController.java:19) ~[classes!/:0.0.1-SNAPSHOT]
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:577) ~[na:na]
        at 
    ...

Why is this.request null? Giving the popularity of https://stackoverflow.com/a/3324233/2287586, seems like this should work, what am I missing?

Renato
  • 233
  • 2
  • 8
  • Did you expect a `Foo` bean to somehow be injected into the call to `getGet`? Why? Did you mean for `Foo` to be an `@Autowired` field instead? – Sotirios Delimanolis Sep 10 '22 at 00:25
  • Yes, and the part of Foo bean being injected seems to be working. My goal is to have injected a request-scoped objects which is prepared based on the request headers. I have tried adding `@Autowired` annotation, but got the same result. Note the error specifically says `this.request` to be null, not `foo`. – Renato Sep 10 '22 at 16:06
  • Your method is a request handler. The Spring MVC layer does not perform injection or anything like that. What you’re getting there is probably an instance created implicitly for the body of the request (which I guess is empty). It’s not a bean and its fields will not be post processed for injection. – Sotirios Delimanolis Sep 10 '22 at 16:13

1 Answers1

-1

It doesn't work becuse it needs to be a Singleton bean and you have @RequestScope. Maybe try this:

((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
Ocirederf
  • 113
  • 1
  • 6
  • HttpServletRequest sounds like to be specialized on Request, so why wouldn't it be available on @RequestScope? – Renato Sep 10 '22 at 16:00
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 15 '22 at 06:58