DEV Community

Cover image for πŸŒ’ JakartaEE JSON-B 🐝 Retrofit2 Converter
Carlos Chacin β˜•πŸ‘½
Carlos Chacin β˜•πŸ‘½

Posted on • Originally published at carloschac.in on

πŸŒ’ JakartaEE JSON-B 🐝 Retrofit2 Converter

Retrofit is pluggable allowing different serialization formats and their libraries to be used for converting Java types to their HTTP representation and parsing HTTP entities back into Java types.

These are called converters, and Retrofit includes a few first-party modules for popular frameworks

Just for fun, I created a Retrofit2 Converter.Factory for JakartaEE Json-B.

Usage

Add the dependencies:

Retrofit2
<dependency>
  <groupId>com.squareup.retrofit2</groupId>
  <artifactId>retrofit</artifactId>
  <version>2.9.0</version>
</dependency>

Enter fullscreen mode Exit fullscreen mode
OkHttp3
<dependency>
  <groupId>com.squareup.okhttp3</groupId>
  <artifactId>okhttp</artifactId>
  <version>3.14.9</version>
</dependency>

Enter fullscreen mode Exit fullscreen mode
JakartaEE Json Binding Reference Implementation
<dependency>
  <groupId>org.eclipse</groupId>
  <artifactId>yasson</artifactId>
  <version>1.0.8</version>
</dependency>

Enter fullscreen mode Exit fullscreen mode
JsonB Retrofit2 Converter
<dependency>
  <groupId>io.github.cchacin</groupId>
  <artifactId>jsonb-retrofit-converter</artifactId>
  <version>1.0.2</version>
</dependency>

Enter fullscreen mode Exit fullscreen mode

Add a converter factory when building your Retrofit instance using thecreate method:

var retrofit = new Retrofit.Builder()
    .baseUrl("https://example.com/")
    .addConverterFactory(JsonbConverterFactory.create())
    .build();

Enter fullscreen mode Exit fullscreen mode

Alternatively, you can pass an instance of javax.json.bind.Jsonb:

var jsonb = JsonbBuilder.create(/* configure your jsonb instance here */ );
var retrofit = new Retrofit.Builder()
    .baseUrl("https://example.com/")
    .addConverterFactory(JsonbConverterFactory.create(jsonb))
    .build();

Enter fullscreen mode Exit fullscreen mode

Now we are able to use JakartaEE JsonB to serialize and deserialize POJOs using Retrofit2

The code is available on Github:

GitHub logo cchacin / jsonb-retrofit-converter

JakartaEE Json-B Converter for Retrofit2

JakartaEE Json-B Retrofit2 Converter

A Retrofit 2 Converter.Factory for JakartaEE Json-B.

Usage

Add a converter factory when building your Retrofit instance using the create method:

var retrofit = new Retrofit.Builder()
    .baseUrl("https://example.com/")
    .addConverterFactory(JsonbConverterFactory.create())
    .build();
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can pass an instance of javax.json.bind.Jsonb:

var jsonb = JsonbBuilder.create(/* configure your jsonb instance here */ );
var retrofit = new Retrofit.Builder()
    .baseUrl("https://example.com/")
    .addConverterFactory(JsonbConverterFactory.create(jsonb))
    .build();
Enter fullscreen mode Exit fullscreen mode

Download

Download the latest JAR or grab via Gradle:

implementation 'io.github.cchacin:jsonb-retrofit-converter:1.0.2'
Enter fullscreen mode Exit fullscreen mode

or Maven:

<dependency>
  <groupId>io.github.cchacin</groupId>
  <artifactId>jsonb-retrofit-converter</artifactId>
  <version>1.0.2</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Snapshots of the development version are available in Sonatype's snapshots repository.

License

Copyright 2020 Carlos Chacin
Licensed under the Apache License, Version 2.0 (the "License");
you
…

Latest comments (0)