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
- Host: GitHub
- URL: https://github.com/ghusta/retrofit-converter-validation
- Owner: ghusta
- License: apache-2.0
- Created: 2023-02-05T15:25:46.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-08-21T22:03:27.000Z (10 months ago)
- Last Synced: 2025-08-21T23:49:07.062Z (10 months ago)
- Topics: bean-validation, retrofit2
- Language: Java
- Homepage:
- Size: 81.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.adoc
- Changelog: CHANGELOG.md
- License: LICENSE
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