Skip to content

Java / Kotlin

Getting started

1. Install the dependency

Add the typedrest Maven artifact to your project.

<dependency>
    <groupId>net.typedrest</groupId>
    <artifactId>typedrest</artifactId>
    <version>VERSION</version>
</dependency>
implementation 'net.typedrest:typedrest:VERSION'

2. Create an entry endpoint

Create an EntryEndpoint pointing at your API:

import net.typedrest.endpoints.EntryEndpoint;
import java.net.URI;

EntryEndpoint client = new EntryEndpoint(URI.create("https://example.com/api/"));
import net.typedrest.endpoints.EntryEndpoint
import java.net.URI

val client = EntryEndpoint(URI.create("https://example.com/api/"))

3. Define your client class

Extend EntryEndpoint and expose your API's resources as methods or properties:

import net.typedrest.endpoints.generic.CollectionEndpoint;
import net.typedrest.endpoints.generic.CollectionEndpointImpl;

class MyClient extends EntryEndpoint {
    public MyClient(URI uri) {
        super(uri);
    }

    public CollectionEndpoint<Contact> getContacts() {
        return new CollectionEndpointImpl<>(this, "./contacts", Contact.class);
    }
}
import net.typedrest.endpoints.generic.CollectionEndpoint
import net.typedrest.endpoints.generic.CollectionEndpointImpl

class MyClient(uri: URI) : EntryEndpoint(uri) {
    val contacts: CollectionEndpoint<Contact>
        get() = CollectionEndpointImpl(this, "./contacts", Contact::class.java)
}

4. Use the client

Use your client to interact with the API:

MyClient client = new MyClient(URI.create("https://example.com/api/"));

// Read all contacts
List<Contact> contacts = client.getContacts().readAll();

// Create a new contact
ElementEndpoint<Contact> newContact = client.getContacts().create(new Contact("Smith"));

// Read a specific contact
Contact contact = newContact.read();
val client = MyClient(URI.create("https://example.com/api/"))

// Read all contacts
val contacts: List<Contact> = client.contacts.readAll()

// Create a new contact
val newContact: ElementEndpoint<Contact> = client.contacts.create(Contact("Smith"))

// Read a specific contact
val contact: Contact = newContact.read()

5. Next steps

Additional packages

typedrest-reactive
Adds support for streaming with ReactiveX (Rx).
Create endpoints using the types in the net.typedrest.endpoints.reactive package.

typedrest-serializers-jackson
Adds support for serializing using Jackson instead of kotlinx.serialization.
Pass new JacksonJsonSerializer() to the EntryEndpoint constructor.

typedrest-serializers-moshi
Adds support for serializing using Moshi instead of kotlinx.serialization.
Pass new MoshiJsonSerializer() to the EntryEndpoint constructor.

See also