https://github.com/mwiede/feign-validation
A library for Feign containing a decorator which makes it possible to validate reponses using validation-api (JSR349 and JSR380)
https://github.com/mwiede/feign-validation
annotations bean-validation decorators feign jax-rs jsr349 jsr380 validation
Last synced: 5 months ago
JSON representation
A library for Feign containing a decorator which makes it possible to validate reponses using validation-api (JSR349 and JSR380)
- Host: GitHub
- URL: https://github.com/mwiede/feign-validation
- Owner: mwiede
- License: apache-2.0
- Created: 2018-08-08T12:07:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2020-01-05T20:00:32.000Z (over 6 years ago)
- Last Synced: 2025-07-05T12:47:06.169Z (12 months ago)
- Topics: annotations, bean-validation, decorators, feign, jax-rs, jsr349, jsr380, validation
- Language: Java
- Homepage:
- Size: 37.1 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# feign-validation [](https://travis-ci.org/mwiede/feign-validation) [](https://search.maven.org/search?q=g:%22com.github.mwiede%22%20AND%20a:%22feign-validation%22)
A library for Feign containing a decorator which makes it possible to validate reponses using bean validation api 1.1 ([JSR 349](https://jcp.org/en/jsr/detail?id=349)) or bean validation 2.0 ([JSR 380](https://jcp.org/en/jsr/detail?id=380)).
While Jersey as the JAX-RS implementation provides [Bean Validation Support](https://jersey.github.io/documentation/latest/bean-validation.html), [Feign](https://github.com/OpenFeign/feign) acts as a client and til now only supports a limited set of annotations
in it's [JAX-RS module](https://github.com/OpenFeign/feign/tree/master/jaxrs2), 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](https://beanvalidation.org/), 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:
```java
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
@Size(min = 1)
@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);
}
}
```
## Download
With JSR-380 support:
```
com.github.mwiede
feign-validation
2.0
```
With JSR-349 support:
```
com.github.mwiede
feign-validation
1.0
```