Spring Netflix Fegin, an alternative to the classic RestTemplate

I’ve been using the Netflix OSS stack for a while now; and I have to say I’m really impressed. These guys know how to do OpenSource and they know how to do it well.

Create a client to consume an external resource via fiegn? Easy


@FeignClient(name = "TodoClient", url = "https://api.todo.com/v1", configuration = TodoClientConfig.class)
public interface TodoClient {

	@RequestMapping(method = RequestMethod.GET, value = "/external-pojo")
	public List<Todo> findAll();
}

@Configuration
public class TodoClientConfig {

	@Bean
    public Decoder decoder() {
        ObjectFactory<HttpMessageConverters>objectFactory = () -> new HttpMessageConverters(new MappingJackson2HttpMessageConverter());
        return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
    }
}

Need to add some authentication tokens to the header? pfft piece of cake:

@Component
public class AuthRequestInterceptor implements RequestInterceptor {

	@Override
	public void apply(RequestTemplate template) {
		template.header("my-header", "header-token");
	}
}

Checkout the Github Repo and the Spring Cloud Netflix Project.

Leave a comment