An open API service indexing awesome lists of open source software.

https://github.com/ghusta/retrofit-converter-validation

Retrofit 2 Converter for Bean Validation
https://github.com/ghusta/retrofit-converter-validation

bean-validation retrofit2

Last synced: 6 days ago
JSON representation

Retrofit 2 Converter for Bean Validation

Awesome Lists containing this project

README

          

= Retrofit 2 Converter for Bean Validation

image:https://github.com/ghusta/retrofit-converter-validation/actions/workflows/maven.yml/badge.svg[Java CI with Maven,link=https://github.com/ghusta/retrofit-converter-validation/actions/workflows/maven.yml]
image:https://img.shields.io/github/v/release/ghusta/retrofit-converter-validation?sort=semver&logo=GitHub[GitHub release,link=https://github.com/ghusta/retrofit-converter-validation/releases]
image:https://img.shields.io/maven-central/v/io.github.ghusta.retrofit2/retrofit-converter-validation.svg?label=Maven%20Central[Maven Central,link=https://search.maven.org/search?q=g:%22io.github.ghusta.retrofit2%22%20AND%20a:%22retrofit-converter-validation%22]

== Overview

This library enables validating beans with https://beanvalidation.org/[Bean Validation] using https://square.github.io/retrofit/[Retrofit 2] thanks to its converter feature.

== Getting started

=== Add dependency

With Maven :

[source,xml]
----

io.github.ghusta.retrofit2
retrofit-converter-validation
1.0.0

----

=== Configure Retrofit and add converters

For example :

[source,java]
----
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(ValidationConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
----

NOTE: The declaration order of converters is important. `ValidationConverterFactory` must be declared before JSON converters.

=== Use Annotations

- On DTO (`@NotNull`, `@Size`, `@Max`, etc.)
- On API method parameters (`@Valid`, before `@Body`)
- Add `@ValidationGroups(groups = {...})` for specific validation

== References

- https://square.github.io/retrofit/
- https://beanvalidation.org/2.0/[Jakarta Bean Validation 2.0]
- https://futurestud.io/tutorials/retrofit-2-implementing-custom-converters
- https://futurestud.io/tutorials/retrofit-2-introduction-to-multiple-converters
- https://stackoverflow.com/questions/tagged/retrofit