FazoM
FazoM

Reputation: 4956

Spring - inject value for request header name

There is a problem with Spring and injecting request header name value into controller.
Here is the code:

@Controller
public class ApiController {

    @Value("${param.header_name}")
    private String param;

    @RequestMapping(value = "/**")
    public void handleApiRequest(final HttpServletRequest request, final HttpServletResponse response,
        @RequestHeader(value = param) final String param)

Properties are defined using @PropertySource and PropertySourcesPlaceholderConfigurer.

The problem is:

"The value for annotation attribute RequestHeader.value must be a constant expression."

But it is not possible to inject a value to a constant (final static) field. Is there a workaround for this? I would like to use RequestHeader annotation / mapping and property file to define the header name.

Upvotes: 5

Views: 4867

Answers (1)

sp00m
sp00m

Reputation: 48807

Values used in annotations must be resolvable at compile-time, but param's value can only be determined at runtime

The only solution I see is to use HttpServletRequest.getHeader(String):

String headerValue = request.getHeader(param);

Upvotes: 4

Related Questions