Populating Headers Using Configuration

The @Header annotation can be declared at the type level and is repeatable such that it is possible to drive the request headers sent via configuration using annotation metadata.

The following example serves to illustrate this:

Defining Headers via Configuration

  1. @Client("/pets")
  2. @Header(name="X-Pet-Client", value="${pet.client.id}")
  3. public interface PetClient extends PetOperations {
  4. @Override
  5. Single<Pet> save(String name, int age);
  6. @Get("/{name}")
  7. Single<Pet> get(String name);
  8. }

Defining Headers via Configuration

  1. @Client("/pets")
  2. @Header(name="X-Pet-Client", value='${pet.client.id}')
  3. interface PetClient extends PetOperations {
  4. @Override
  5. Single<Pet> save(String name, int age)
  6. @Get("/{name}")
  7. Single<Pet> get(String name)
  8. }

Defining Headers via Configuration

  1. @Client("/pets")
  2. @Header(name = "X-Pet-Client", value = "\${pet.client.id}")
  3. interface PetClient : PetOperations {
  4. override fun save(name: String, age: Int): Single<Pet>
  5. @Get("/{name}")
  6. operator fun get(name: String): Single<Pet>
  7. }

The above example defines a @Header annotation on the PetClient interface that reads a property using property placeholder configuration called pet.client.id.

In your application configuration you then set the following in application.yml to populate the value:

Configuring Headers in YAML

  1. pet:
  2. client:
  3. id: foo

Alternatively you can supply a PET_CLIENT_ID environment variable and the value will be populated.