Introducing Code-first Java SDK for Kalix

By Kalix
The Java SDK for Kalix offers a developer experience that is familiar to both Java and Spring Boot users.

It allows developers to assemble a Kalix application by annotating Java classes. Compared to other SDKs for Kalix, it frees the developer from describing their API with Protocol Buffers.

When using the Kalix Java SDK, your services are exposed using Spring REST annotations and serialization is backed by the ubiquitous Jackson library. And, most importantly, you can start coding directly in Java.

To give you a quick preview of how it looks to define an Entity, the code below shows a Kalix ValueEntity for an imaginary Customer model:

    package customer;
    
    import kalix.javasdk.valueentity.ValueEntity;
    import kalix.javasdk.annotations.EntityKey;
    import kalix.javasdk.annotations.EntityType;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestBody;

    @EntityType("customer")
    public class CustomerEntity extends ValueEntity {

        record Customer(String name, String email){}
        record CreateCustomer(String name, String email){}
        
        @EntityKey("customer_id")
        @PostMapping("/customers/{customer_id}")
        public Effect createCustomer(@RequestBody CreateCustomer create) {
            return effects()
            .updateState(new Customer(create.name, create.email))
            .thenReply("done");
        }
    }

When deployed to Kalix, clients can call the service using any http client. The createCustomer method is exposed as a regular REST endpoint using Spring’s REST annotations.

To query Customers by name, you can write:

    package customer;

    import kalix.javasdk.view.View;
    import kalix.javasdk.annotations.Query;
    import kalix.javasdk.annotations.Subscribe;
    import kalix.javasdk.annotations.Table;
    import org.springframework.web.bind.annotation.GetMapping;
    import reactor.core.publisher.Flux;

    @Table("customers")
    @Subscribe.ValueEntity(CustomerEntity.class)
    public class CustomerByNameView extends View {
        
        @GetMapping("/customers/by-name/{name}")
        @Query("SELECT * FROM customers WHERE name = :name")
        public Flux findCustomers(String name) {
            return null;
        }
    }
    

The Kalix Java SDK detects all Kalix components under the main package and registers them automatically to Kalix.

That’s basically all that is needed to implement an Entity with query capabilities in Kalix. Just start coding!

You can try it out yourself by starting a new project using the Maven archetype or by downloading the quickstart sample.

To start a new Maven project

    mvn archetype:generate  
    -DarchetypeGroupId=io.kalix 
    -DarchetypeArtifactId=kalix-spring-boot-archetype
    -DarchetypeVersion=1.1.1
    

To download a fully-functional quickstart project

    kalix quickstart download customer-registry-java

You can find more details in the new Kalix Java SDK documentation.

Share
Share

Featured Articles

Kyndryl & Stellantis Drive CX, Efficiency and Sustainability

Kyndryl will help modernise Stellantis’ IT infrastructure and enhance its focus on digital, cybersecurity, new business models & customer-centric solutions

The State of AI: Remaining Competitive in a Fast-Paced World

With insights from Sreekar Krishna, Head of AI at KPMG, we explore the importance of AI upskilling and how businesses can best benefit from the technology

MWC24: Mimik Hybrid Edge Cloud Drives Cognitive Internet Era

Siavash Alamouti, mimik Co-founder & Executive Chairman, explains how its hybrid edge cloud platform enables the transition to the Cognitive Internet Era

Dell Technologies: Powering Reliable Global Connectivity

Cloud Computing

MWC Barcelona 2024: Unveiling the Future of Technology

Digital Transformation

Google Gemma: An AI Model Small Enough to Run on a Laptop

AI & Machine Learning