How to send multiple headers using Open Feign ?

If you are consuming a REST service in Spring using Open Feign , you may come across scenarios where multiple headers need to be set.

In the previous post:

How to invoke a basic authenticated REST service using Open Feign?

I explained how to set Authorization header while consuming a basic authenticated REST service.

You need to use the annotation @RequestHeader in one of the parameters and pass the header along with it in the method declared inside Feign Interface.

What if you want to send multiple headers?

Just repeat the annotation!

Here is an example , in addition to the authorization header I am sending a custom header “color-of-my-bike” along with my method invocation.

My method declaration inside the feign interface looks like this :

package com.springboot.openfeign;

import java.util.List;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(name = "personclient", url = "http://localhost:8090/")
public interface PersonClient {

	@RequestMapping(method = RequestMethod.GET, value = "/persons")
	List<Person> getPersons(@RequestHeader("Authorization") String header,
			@RequestHeader("color-of-my-bike") String bikeColor);
}

In addition to the ‘Authorization’ header I am sending another custom header “color-of-my-bike”:

@RequestHeader("color-of-my-bike") String bikeColor

I am invoking the above method in my application like this :

package com.springboot.openfeign;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RestConsumer {

	@Autowired
	private PersonClient client;

	@GetMapping("/getAllPersons")
	public List<Person> getAllPersons() {

		String username = "admin";
		String password = "admin";

		byte[] encodedBytes = Base64Utils.encode((username + ":" + password).getBytes());

		String authHeader = "Basic " + new String(encodedBytes);

		return client.getPersons(authHeader, "blue");
	}
}

As you see I am just sending the value “blue” for the custom header “color-of-my-bike”.

Let me read this in the REST service which I am consuming :

package com.springboot.simplerest;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

	@GetMapping("/persons")
	public List<Person> getPersons(@RequestHeader("color-of-my-bike") String value) {

		System.out.println(value);

		List<Person> personList = new ArrayList<Person>();

		Person person = new Person(1, "Vijay", 34, 97565656);
		Person person2 = new Person(2, "Sam", 30, 908967678);
		Person person3 = new Person(3, "Vishwas", 24, 909787888);

		personList.add(person);
		personList.add(person2);
		personList.add(person3);

		return personList;
	}

}

I am reading the header using the @RequestHeader (same annotation!) annotated parameter:

@RequestHeader("color-of-my-bike") String value

It prints the value “blue” in the console.


Posted

in

,

by

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading