1

I am using spring boot for creating microservices. I need to implement request scope beans as I get some information in header and need this to be available across all the classes for that particular request. Below is what I did, but I get null pointer error.

@Component
@RequestScope(proxyMode = ScopedProxyMode.TARGET_CLASS)
public Class RequestHeaderInfo {
   private String appInfo;
   ...
}

@Component
public class RequestFilter implements Filter {
    @Autowired
    private RequestHeaderInfo requestHeaderInfo;
  
    public void doFilter(ServletRequest req,....) {
        HTTPServletRequest request = (HTTPServletRequest) req;
        requestHeaderInfo.setAppInfo(request.getHeader("appInfo"))   //throws null pointer error here
        ....
    }
}  

@Contoller
public class RestController {

    @Autowired
    private RequestHeaderInfo requestHeaderInfo;
}

I want this request header info object to be available throughout the particular request. In my filter class it throws null pointer error. Am I on the right track implementing request scoped bean?

halfer
  • 19,824
  • 17
  • 99
  • 186
Lolly
  • 34,250
  • 42
  • 115
  • 150
  • 1
    Why did you annotate `RequestFilter` with `@Configuration`? Try `@Component` – crizzis Mar 31 '21 at 10:41
  • Please, try to include `@ComponentScan("package.that.contains.requestheaderinfo")`, to be sure that `RequestHeaderInfo` is correctly located in its package. It should be appropriate to define a `@Configuration` with that information, and annotate the filter as `@Component` as @crizzis suggested – jccampanero Mar 31 '21 at 10:46
  • @crizzis you are right, it should be component. I have edited it. – Lolly Mar 31 '21 at 11:06
  • I don't understand the reasoning behind it. Why don't you use the annotation `@RequestHeader` in the controller and get the values you need? – Marcos Barbero Mar 31 '21 at 11:30
  • 1
    @Marcos Barbero Cant speak for OP, but I can think of multiple reasons. Maybe you would like to have that value in a service which is 3 layers deep for example. And you would not like to pass this stuff through arguments. I have not used `@RequestScope` myself, but I have implemented my own custom solutions for problems like that. For example, maybe your controllers are autogenerated by OpenApi and you can't even add custom code there. – Tarmo Mar 31 '21 at 12:00

1 Answers1

0

In our project (Spring boot multi-module and not microservices) we are using an Interceptor class to filter the request.

@Component
public class RequestInterceptor implements org.springframework.web.servlet.HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        String appInfo = request.getHeader("appInfo");
    }
    
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
        throws Exception {
    }
}

Plus our Bean with the request scope is configured like this

@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component
public class RequestBean {

}

Hope this helps.

Arun Sudhakaran
  • 2,167
  • 4
  • 27
  • 52