Response validation when using Feign as http client wrapper

If you have the requirement of validating the response of a rest interface, a good opportunity in Java is to use the bean validation api and its annotations. On top, if you use OpenFeign wrapper to define the client in your application, you can now assign those validation annotations directly to your interface.

If you have already seen in my previous blogpost about Feign Outbound metrics, I am trying to extend feign with some decorator classes to add missing functionality. metrics-feign is a library, which can be used to report outbound-metrics out of any invocation to the feign wrapper.

Now I created another library, which makes it possible to validate the response using bean validation api (JSR349). Its called feign-validation and you can find it at github or maven central.

The only thing you have to do after setting it up is to add annotations to the interface class you are using. Here is an example:

interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
@Size(min = 1, max = 3)
List contributors(@Param("owner") String owner, @Param("repo") String repo);


static class Contributor {
@Size(min = 10)
String login;
int contributions;

public static void main(String... args) {

Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

GitHub github = ExtendedFeign.builder(validator)//
.decoder(new GsonDecoder()).target(GitHub.class, "");

// Fetch and print a list of the contributors to this library.
try {
List contributors = github.contributors("OpenFeign", "feign");
for (Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
} catch (ConstraintViolationException ex) {


If you would run this class, you can see, that the response is being validated against the given annotations and all violations a printed to stdout.

  • First, the @Size annotation leads to a constraint violation, because the size of the list is checked, whether it has at least one or maximim three items.
  • Because of @Valid, each item in the list of contributors is validated
  • @Size in Contributor class is set to verify, that the login name must have at least 10 characters. Because not all contributors have such a long name, a few more violations are reported
  • as seen, you can use any annotation from the api to fulfil the need

So what are you waiting for?

  • add



    to your dependencies

  • exchange feign.Feign.builder with com.github.mwiede.feign.validation.ExtendedFeign.builder
  • and add annotations like explained above.
Please follow and like us: