0

I want to set value from application properties for @getMapping headers. I was trying this

@GetMapping(path = "/test/classify/{id}",
        headers = "${partners.api-key}")

But it doesn't work. Is it possible to do that? Thanks in advance.

Olyalya
  • 81
  • 1
  • 1
  • 2

1 Answers1

0

hi I think you should use @Value to get property and HttpServletResponse to set the header like this:

@Value("${partners.api-key}")
private String apiKey;

@GetMapping(path = "/test/classify/{id}")
public Object get(HttpServletResponse response){
    .....
    response.addHeader("api-key", apiKey);
    return object;
}

  • That is not the same as `@GetMapping(..., headers = "...")` because the annotation only maps requests that contain the specified header, see [Javadoc](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html#headers--). – Roland Weisleder Apr 28 '21 at 06:43