Table of Contents
This library adds support for Jackson’s @JsonView
with Retrofit 2, which is not available in the original converter-jackson Retrofit 2 library.
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();
}
-
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();
Inspired by support of @JsonView
in Spring MVC.