Skip to content

ghusta/retrofit-converter-jackson-extended

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Retrofit 2 Converter for Jackson extended with additional features

Build with Maven GitHub release Maven Central

Table of Contents

Overview

This library adds support for Jackson’s @JsonView with Retrofit 2, which is not available in the original converter-jackson Retrofit 2 library.

Usage

First, import the library :

    <dependency>
        <groupId>io.github.ghusta.retrofit2</groupId>
        <artifactId>retrofit-converter-jackson-extended</artifactId>
        <version>1.1.0</version>
    </dependency>

For example, you can add a @JsonView annotation on a request method :

public interface MyApi {

    @GET("users")
    @JsonView(Views.Public.class) (1)
    CompletableFuture<List<User>> getUsers();

}
  1. Annotation @JsonView with view class as attribute

The different views can be managed in a unique class like this :

public class Views {
    public interface All { }
    public interface Public { }
}

You will have to configure Retrofit to use this custom converter that way :

ObjectMapper objectMapper = JsonMapper.builder()
    ...
    .build;

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.acme.com/")
    ...
    .addConverterFactory(JacksonExtendedConverterFactory.create(objectMapper))
    .build();

Inspiration

Inspired by support of @JsonView in Spring MVC.