Skip to content
A library for Feign containing a decorator which makes it possible to validate reponses using validation-api (JSR349)
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
.travis.yml
LICENSE
README.md
pom.xml

README.md

feign-validation travis status Maven Central

A library for Feign containing a decorator which makes it possible to validate reponses using bean validation api 1.1 (JSR 349).

While Jersey as the JAX-RS implementation provides Bean Validation Support, Feign acts as a client and til now only supports a limited set of annotations in it's JAX-RS module, meaning @javax.validation.Valid or @javax.validation.groups.ConvertGroup are not supported.

This library contains a decorator class, which can be setup within invocationHandlerFactory to retrieve additional validation.

Motivation

Sometimes it is required, that a client also validates the response it got from the providing service/endpoint. With JSR-349 bean validation, Java provides a nice api for validation purposes. Just by adding annotations like @NotNull or @Email, contraints can be validated very easily.

Example

Here is how you can activate bean validation within Feign wrapper client:

interface GitHub {
    @RequestLine("GET /repos/{owner}/{repo}/contributors")
    @Size(min = 1)
    @Valid
    List<Contributor> 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<Contributor> 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);
    }

}

Download

<dependency>
  <groupId>com.github.mwiede</groupId>
  <artifactId>feign-validation</artifactId>
  <version>1.0</version>
</dependency>
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.