Skip to content

ghusta/retrofit-converter-validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Retrofit 2 Converter for Bean Validation

Java CI with Maven GitHub release Maven Central

Overview

This library enables validating beans with Bean Validation using Retrofit 2 thanks to its converter feature.

Getting started

Add dependency

With Maven :

<dependency>
    <groupId>io.github.ghusta.retrofit2</groupId>
    <artifactId>retrofit-converter-validation</artifactId>
    <version>1.0.0</version>
</dependency>

Configure Retrofit and add converters

For example :

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