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)
@Valid
List contributors(@Param("owner") String owner, @Param("repo") String repo);

}

static class Contributor {
@NotNull
@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, "https://api.github.com");

// 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) {
ex.getConstraintViolations().forEach(System.out::println);
}

}

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
    <dependency>
      <groupId>com.github.mwiede</groupId>
      <artifactId>feign-validation</artifactId>
      <version>1.0</version>
    </dependency>

    and

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.4.2.Final</version>
    </dependency>

    to your dependencies

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

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.