0

I want to read a property from application.properties and set as default value to @RequestHeader to an API endpoint. My properties look like item.includePrice.value=Y

ItemController.java

@PostMapping("/item")
public String postBody(@RequestHeader String includePrice, @RequestBody SomeRequest someRequest) {
   if(includePrice != null) 
       return "You searched for " + someRequest.getItemName;
}

I have referred below questions and tried two approaches.

First approach is, tried using a custom interceptor. But, in order to set the default value for includePrice after reading the property from application.properties under the preHandle method, I do not see addHeader method for HttpServletRequest.

Application Properties value in Spring boot interceptor

Second approach is used below resources and implemented a MutableHttpServletRequest that extends HttpServletRequestWrapper and added the header.

Adding an HTTP Header to the request in a servlet filter

How to add HttpServletRequest headers in HandlerInterceptorAdapter?

Expectation: Regardless the API request sends the includePrice header value or not, at all times, the property should be read from the application.properties and set the default value to my HttpServletRequest.

Jens
  • 67,715
  • 15
  • 98
  • 113
  • Couldn't you just define the header as optional and handle it within your application code? – Dinosaur-Guy Jan 13 '23 at 06:51
  • I can, but my requirement is to read all the global properties and set it to the request instead of setting the default value in @RequestHeader – psprogrammer Jan 13 '23 at 16:52

0 Answers0