com.github.mwiede:feign-validation

A decorator wrapping Feign client method handlers in order to provide validation on response object.

License

License

Categories

Categories

Feign Net HTTP Clients
GroupId

GroupId

com.github.mwiede
ArtifactId

ArtifactId

feign-validation
Last Version

Last Version

2.0
Release Date

Release Date

Type

Type

jar
Description

Description

com.github.mwiede:feign-validation
A decorator wrapping Feign client method handlers in order to provide validation on response object.
Project URL

Project URL

https://github.com/mwiede/feign-validation
Source Code Management

Source Code Management

https://github.com/mwiede/feign-validation

Download feign-validation

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.mwiede/feign-validation/ -->
<dependency>
    <groupId>com.github.mwiede</groupId>
    <artifactId>feign-validation</artifactId>
    <version>2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.mwiede/feign-validation/
implementation 'com.github.mwiede:feign-validation:2.0'
// https://jarcasting.com/artifacts/com.github.mwiede/feign-validation/
implementation ("com.github.mwiede:feign-validation:2.0")
'com.github.mwiede:feign-validation:jar:2.0'
<dependency org="com.github.mwiede" name="feign-validation" rev="2.0">
  <artifact name="feign-validation" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.mwiede', module='feign-validation', version='2.0')
)
libraryDependencies += "com.github.mwiede" % "feign-validation" % "2.0"
[com.github.mwiede/feign-validation "2.0"]

Dependencies

compile (2)

Group / Artifact Type Version
io.github.openfeign : feign-core jar 10.4.0
javax.validation : validation-api jar 2.0.1.Final

test (6)

Group / Artifact Type Version
org.hibernate.validator : hibernate-validator jar 6.0.17.Final
org.glassfish : javax.el jar 3.0.0
junit : junit jar 4.12
org.mockito : mockito-core jar 2.27.0
io.github.openfeign : feign-gson jar 10.4.0
io.github.openfeign : feign-okhttp jar 10.4.0

Project Modules

There are no modules declared in this project.

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) or bean validation 2.0 (JSR 380).

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. With JSR-380 you even get support for new date/time data types (JSR 310) and more.

Versions

feign-validation Version JSR Validation Api Version
1.x JSR-349 support javax.validation:validation-api:1.1.0.Final
2.x and beyond JSR-380 support javax.validation:validation-api:2.0.1.Final

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

With JSR-380 support:

<dependency>
  <groupId>com.github.mwiede</groupId>
  <artifactId>feign-validation</artifactId>
  <version>2.0</version>
</dependency>

With JSR-349 support:

<dependency>
  <groupId>com.github.mwiede</groupId>
  <artifactId>feign-validation</artifactId>
  <version>1.0</version>
</dependency>

Versions

Version
2.0
1.0